What does ?: do in regex

前端 未结 3 1244
北荒
北荒 2021-01-01 11:05

I have a regex that looks like this

/^(?:\\w+\\s)*(\\w+)$*/

What is the ?:?

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-01 11:26

    It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s), even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.

    You're still looking for a specific pattern (in this case, a single whitespace character following at least one word), but you don't care what's actually matched.

提交回复
热议问题