2020年8月19日星期三

Regex Match Word Pattern By (?:^|\W)word(?:$|\W) or \bword\b As Word Boundaries.

1. Regex Match Word Pattern By (?:^|\W)word(?:$|\W)


(?:^|\W)  : Start with line starter or Not alphabet and _.
(?:$|\W)   : End with line ender or Not alphabet and _.

Examples:
pattern : (?:^|\W)(word)(?:$|\W)

input:
user/edit.js:word
user/edit.js:aword
user/edit.js:words
user/edit.js:abc,word
user/edit.js:abc,words

matches:
user/edit.js:word
user/edit.js:abc,word

2. Regex Match Word Pattern By \bword\b

Word boundaries: \bword\b
The results are same as 1.
matches:
user/edit.js:word
user/edit.js:abc,word

Non-boundaries:
\Bword\B


没有评论: