我有一個很長的無意義字符串,其中每個字符都是數(shù)字[0-9]或小寫字母[a-z],如下所示
"0z8b816ne139z1b948bjk50f9498t139gjj90t7tb3509w6h0r7tbp"
我想要一個正則表達(dá)式,可以匹配字符串中出現(xiàn)超過一次的非連續(xù)模式 我希望輸出結(jié)果如下所示
粗體部分是匹配的部分
"0z8b816ne139z1b948bjk50f9498t139gjj90t7tb3509w6h0r7tbp"
正則表達(dá)式:(..+)(?=.*?(\1))
const regex = /(..+)(?=.*?())/gm; // 使用RegExp構(gòu)造函數(shù)的替代語法 // const regex = new RegExp('(..+)(?=.*?(\1))', 'gm') const str = `0z8b816ne139z1b948bjk50f9498t139gjj90t7tb3509w6h0r7tbp `; let m; while ((m = regex.exec(str)) !== null) { // 避免零寬匹配導(dǎo)致無限循環(huán) if (m.index === regex.lastIndex) { regex.lastIndex++; } // 可以通過`m`變量訪問結(jié)果 m.forEach((match, groupIndex) => { console.log(`找到匹配,第${groupIndex}組:${match}`); }); }