本文實例講述了JS正則表達式修飾符global(/g)用法。分享給大家供大家參考,具體如下:
/g修飾符代表全局匹配,查找所有匹配而非在找到第一個匹配后停止。先看一段經(jīng)典代碼:
var str = "123#abc";var noglobal = /abc/i;//非全局匹配模式console.log(re.test(str)); //輸出tureconsole.log(re.test(str)); //輸出tureconsole.log(re.test(str)); //輸出tureconsole.log(re.test(str)); //輸出turevar re = /abc/ig;//全局匹配console.log(re.test(str)); //輸出tureconsole.log(re.test(str)); //輸出falseconsole.log(re.test(str)); //輸出tureconsole.log(re.test(str)); //輸出false
可以看到:當(dāng)使用/g模式的時候,多次執(zhí)行RegExp.test()的輸出結(jié)果會有差別。下面的解釋摘抄自這篇文章《Javascript中正則表達式的全局匹配模式分析》:
在創(chuàng)建正則表達式對象時如果使用了“g”標(biāo)識符或者設(shè)置它了的?global屬性值為ture時,那么新創(chuàng)建的正則表達式對象將使用模式對要將要匹配的字符串進行全局匹配。在全局匹配模式下可以對指定要查找的字符串執(zhí)行多次匹配。每次匹配使用當(dāng)前正則對象的lastIndex屬性的值作為在目標(biāo)字符串中開 始查找的起始位置。lastIndex屬性的初始值為0,找到匹配的項后lastIndex的值被重置為匹配內(nèi)容的下一個字符在字符串中的位置索引,用來標(biāo)識下次執(zhí)行匹配時開始查找的位置。如果找不到匹配的項lastIndex的值會被設(shè)置為0。當(dāng)沒有設(shè)置正則對象的全局匹配標(biāo)志時lastIndex屬性的值始終為0,每次執(zhí)行匹配僅查找字符串中第一個匹配的項。
可以通過下面這段代碼驗證lastIndex在/g模式下的表現(xiàn):
var str = "012345678901234567890123456789";var re = /123/g;console.log(re.lastIndex); //輸出0,正則表達式剛開始創(chuàng)建console.log(re.test(str)); //輸出tureconsole.log(re.lastIndex); //輸出4console.log(re.test(str)); //輸出trueconsole.log(re.lastIndex); //輸出14console.log(re.test(str)); //輸出tureconsole.log(re.lastIndex); //輸出24console.log(re.test(str)); //輸出falseconsole.log(re.lastIndex); //輸出0,沒有找到匹配項被重置
上面我們看到了/g模式對于RegExp.test()函數(shù)的影響,現(xiàn)在我們看下對RegExp.exec()函數(shù)的影響。RegExp.exec()返回一個數(shù)組,其中存放匹配的結(jié)果。如果未找到匹配,則返回值為 null。
var str = "012345678901234567890123456789";var re = /abc/;//exec沒有找到匹配console.log(re.exec(str));//nullconsole.log(re.lastIndex);//0
var str = "012345678901234567890123456789";var re = /123/;var resultArray = re.exec(str);console.log(resultArray[0]);//匹配結(jié)果123console.log(resultArray.input);//目標(biāo)字符串strconsole.log(resultArray.index);//匹配結(jié)果在原始字符串str中的索引
對于RegExp.exec方法,不加入g,則只返回第一個匹配,無論執(zhí)行多少次均是如此;如果加入g,則第一次執(zhí)行也返回第一個匹配,再執(zhí)行返回第二個匹配,依次類推。
var str = "012345678901234567890123456789";var re = /123/;var globalre = /123/g;//非全局匹配var resultArray = re.exec(str);console.log(resultArray[0]);//輸出123console.log(resultArray.index);//輸出1console.log(globalre.lastIndex);//輸出0var resultArray = re.exec(str);console.log(resultArray[0]);//輸出123console.log(resultArray.index);//輸出1console.log(globalre.lastIndex);//輸出0//全局匹配var resultArray = globalre.exec(str);console.log(resultArray[0]);//輸出123console.log(resultArray.index);//輸出1console.log(globalre.lastIndex);//輸出4var resultArray = globalre.exec(str);console.log(resultArray[0]);//輸出123console.log(resultArray.index);//輸出11console.log(globalre.lastIndex);//輸出14
當(dāng)使用/g匹配模式的時候,我們可以通過循環(huán),獲取所有的匹配項。
var str = "012345678901234567890123456789";var globalre = /123/g;//循環(huán)遍歷,獲取所有匹配var result = null;while ((result = globalre.exec(str)) != null){ console.log(result[0]); console.log(globalre.lastIndex);}PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.VeVB.COm/regex/javascript
正則表達式在線生成工具:
http://tools.VeVB.COm/regex/create_reg
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript正則表達式技巧大全》、《JavaScript替換操作技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
新聞熱點
疑難解答