Regex $1, $2, etc

后端 未结 2 1417
轻奢々
轻奢々 2021-02-04 01:40

I\'ve been trying to do some regex operations in PHP, and I\'m not very skilled in this area. It seems that when I use a regex function like preg_replace on a string, I can acce

2条回答
  •  醉梦人生
    2021-02-04 01:50

    They are called backreferences and match grouped elements within the regexp.

    If you surround a section of the regexp with brackets, then you can refer to it in the replace section (or indeed later in the same regexp, by the backreference which corresponds to its position.

    Slash form, or dollar form can be used in replacements:

    \1, \2 == $1, $2
    

提交回复
热议问题