比如
[CM][bt][02][03]
我想匹配 02
我的正則是這樣寫的:
i)(?<=\bCM\b(\D*)?)\d+(?=\D)
但無法匹配成功,請問該怎樣改呢?
感謝各位!
正則本身沒錯...錯的是
PCRE 8.34–8.35 UTF-8 does not support variable repetition inside lookbehind or alternatives of different lengths inside groups inside lookbehind
var str = '[CM][bt][02][03]';
str = str.replace(/\[/gi,"@").replace(/\]/gi,"@");
var reg = /\w+@{2}\d+/gi;
var arrMatches = str.match(reg);
for(var i = 0 ; i < arrMatches.length; i++){
console.log( arrMatches[i].replace(/\D/gi,""));
}
由於[]是正則表達式的元字符,先把它替換成@,查找單詞開始,跟著@@,再數字的表達式,獲取表達式後去除非數字的,最後得到數字02