What does the “?:^” regular expression mean?

后端 未结 6 931
悲&欢浪女
悲&欢浪女 2021-02-01 08:39

I am looking at this sub-expression (this is in JavaScript):

(?:^|.....)

I know that ? means \"zero or one times\" when it fol

6条回答
  •  遇见更好的自我
    2021-02-01 09:21

    Short Answer

    It flags the (parenthetical) group as a non-capturing group.

    Details About This Particular Expression

    The notation for a non-capturing group is:

    (?:)
    

    In the instance you presented, the caret (^) is part of the expression not part of the capturing group notation. And this instance it's not a special character either.

    It looks like they're using an 'or' operator (the pipe) with the caret. So they're looking to match something that is a caret or whatever was on the right of the pipe, but not capture the expression as a group (accomplished with the ?: in the beginning of the grouping characters.

    In General

    Non-capturing groups allow you to group an expression in a way that won't be back-refernceable, and will also increase performance of the expression.

提交回复
热议问题