What is the purpose of the passive (non-capturing) group in a Javascript regex?

后端 未结 5 1802
渐次进展
渐次进展 2021-01-12 11:05

What is the purpose of the passive group in a Javascript regex?

The passive group is prefaced by a question mark colon: (?:group)

In other words

5条回答
  •  悲哀的现实
    2021-01-12 12:01

    Use them when you need a conditional and don't care about which of the choices cause the match.

    Non-capturing groups can simplify the result of matching a complex expression. Here, the group 1 is always the name speaker. Without the non-capturing group, the speaker's name may end up in group 1 or group 2.

    /hello (?:world|foobar )?said (.+)/

提交回复
热议问题