JavaScript使用RegExp進(jìn)行正則匹配的方法
更新時間:2015年07月11日 09:53:30 作者:優(yōu)雅先生
這篇文章主要介紹了JavaScript使用RegExp進(jìn)行正則匹配的方法,實例分析了RegExp對象在進(jìn)行正則匹配時的相關(guān)使用技巧,需要的朋友可以參考下
本文實例講述了JavaScript使用RegExp進(jìn)行正則匹配的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
<script type="text/javascript"> var matchedTimes = 0; //Match one d followed by one or more b's followed by one d //Remember matched b's and the following d //Ignore case myRe = new RegExp("d(b+)(d)", "ig"); // 等價于 myReg = /d(b+)(d)/ig; myArray = myRe.exec("ecDBDsdbbdz"); // ecdbBdbsdbbdz console.log("Regular Expression String: " + myRe.source); console.log("Is global? " + myRe.global); console.log("Ignore case? " + myRe.ignoreCase); console.log("Is mulitiline? " + myRe.multiline); console.log("------------------------------------------------"); logInfo(myArray, myRe); myArray = myRe.exec("ecDBDsdbbdz"); logInfo(myArray, myRe); function logInfo(myArray, myRe) { matchedTimes++; console.log("This is " + matchedTimes + " times match"); console.log("Original String: " + myArray.input); console.log("Match Result Array: [" + myArray + "]"); console.log("The 0-based index of the match in the string: " + myArray.index); console.log("The last matched characters: " + myArray[0]); console.log("The parenthesized substring matches [1]: " + myArray[1]); console.log("The parenthesized substring matches [2]: " + myArray[2]); console.log("The index at which to start the next match: " + myRe.lastIndex); console.log("-----------------------------------------------"); } myRe2 = /^\w+(\d*)$/ig console.log("myRe2: " + myRe2.source); //console.log("myRe2 matches abc1? " + myRe2.test("abc1")); // 加上這行跑跑看結(jié)果,因為是global匹配,所以lastIndex會改變, //所以后面的myRe2.test("abc")當(dāng)然就是false console.log("myRe2 matches abc? " + myRe2.test("abc")); </script>
希望本文所述對大家的javascript程序設(shè)計有所幫助。
您可能感興趣的文章:
- JS正則中的RegExp對象對象
- javascript RegExp對象(正則表達(dá)式)
- JavaScript RegExp 正則表達(dá)式對象詳細(xì)說明
- js正則表達(dá)式講解之index屬性(RegExp對象)
- 詳解JavaScript正則表達(dá)式之RegExp對象
- JavaScript 正則表達(dá)式之RegExp屬性、方法及應(yīng)用分析
- JS正則RegExp.test()使用注意事項(不具有重復(fù)性)
- javascript RegExp 對象屬性與方法和應(yīng)用
- 注意 JavaScript 中 RegExp 對象的 test 方法
- javascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解
- JavaScript引用類型RegExp基本用法詳解