Open lewenweijia opened 4 years ago
(?: ) : 非捕获分组(non-capture group) (?! ) : 反前瞻 (negative lookahead) (?= ) : 正前瞻 (lookahead) (?<= ): 正后瞻 (lookbehind) (?<! ): 反后瞻 (negative lookbehind)
(?: )
(?! )
(?= )
(?<= )
(?<! )
用例:
'hello world'.replace(/[lor]/g, '$1-') 'hello world'.replace(/([lor])/g, (_, c) => c.toUpperCase()) /hello(?!better)/.test('hello world') // true /hello(?!better)/.test('hellobetter') // false /(?<!worst) world/.test('better world') // true /(?<!worst) world/.test('worst world') // false
(?: )
: 非捕获分组(non-capture group)(?! )
: 反前瞻 (negative lookahead)(?= )
: 正前瞻 (lookahead)(?<= )
: 正后瞻 (lookbehind)(?<! )
: 反后瞻 (negative lookbehind)用例: