What does (?: do in a regular expression

后端 未结 4 987
天涯浪人
天涯浪人 2021-02-02 09:57

I have come across a regular expression that I don\'t fully understand - can somebody help me in deciphering it:

^home(?:\\/|\\/index\\.asp)?(?:\\?.+)?$
<         


        
4条回答
  •  盖世英雄少女心
    2021-02-02 10:51

    (?:) creates a non-capturing group. It groups things together without creating a backreference.

    A backreference is a part you can refer to in the expression or a possible replacement (usually by saying \1 or $1 etc - depending on flavor). You can also usually extract them from a match afterwards when using regex in a programming language. The only reason for using (?:) is to avoid creating a new backreference, which avoids incrementing the group number, and saves (a usually negligible amount of) memory

提交回复
热议问题