在 JavaScript 中
console.log(/x(?=b[1-9])/.test('xb2')); // true
console.log(/x(?=b[1-9])$/.test('xb2')); // false
有什么區別?
uj5u.com熱心網友回復:
第一個模式x(?=b[1-9])匹配x,然后跟b一個數字。輸入xb2與此匹配。
第二個模式x(?=b[1-9])$是沖突的,永遠無法匹配任何東西。這個模式說要匹配:
x the letter x
(?=b[1-9]) assert that b and 1-9 follows
$ match end of the input
不可能b[1-9]跟隨x而同時x是輸入的結尾。在您的問題中使用第一個版本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/393477.html
標籤:javascript 正则表达式 regex-lookarounds
