JavaScript使用RegExp進(jìn)行正則匹配的方法
更新時(shí)間:2015年07月11日 09:53:30 作者:優(yōu)雅先生
這篇文章主要介紹了JavaScript使用RegExp進(jìn)行正則匹配的方法,實(shí)例分析了RegExp對象在進(jìn)行正則匹配時(shí)的相關(guān)使用技巧,需要的朋友可以參考下
本文實(shí)例講述了JavaScript使用RegExp進(jìn)行正則匹配的方法。分享給大家供大家參考。具體實(shí)現(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");
// 等價(jià)于 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é)果,因?yàn)槭莋lobal匹配,所以lastIndex會(huì)改變,
//所以后面的myRe2.test("abc")當(dāng)然就是false
console.log("myRe2 matches abc? " + myRe2.test("abc"));
</script>
希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- JS正則中的RegExp對象對象
- javascript RegExp對象(正則表達(dá)式)
- JavaScript RegExp 正則表達(dá)式對象詳細(xì)說明
- js正則表達(dá)式講解之index屬性(RegExp對象)
- 詳解JavaScript正則表達(dá)式之RegExp對象
- JavaScript 正則表達(dá)式之RegExp屬性、方法及應(yīng)用分析
- JS正則RegExp.test()使用注意事項(xiàng)(不具有重復(fù)性)
- javascript RegExp 對象屬性與方法和應(yīng)用
- 注意 JavaScript 中 RegExp 對象的 test 方法
- javascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解
- JavaScript引用類型RegExp基本用法詳解
相關(guān)文章
JavaScript實(shí)現(xiàn)網(wǎng)頁計(jì)算器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
詳解JS如何解決大數(shù)據(jù)下滾動(dòng)頁面卡頓問題
之前遇到不分頁直接獲取到全部數(shù)據(jù),前端滾動(dòng)查看數(shù)據(jù),頁面就挺卡頓的,所以這篇文章來和大家聊聊如何解決這一問題,感興趣的小伙伴可以了解下2023-07-07
小程序點(diǎn)贊收藏功能的實(shí)現(xiàn)代碼示例
這篇文章主要介紹了小程序點(diǎn)贊收藏功能的實(shí)現(xiàn)代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

