JavaScript使用RegExp進(jì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>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- JS正則中的RegExp對(duì)象對(duì)象
- javascript RegExp對(duì)象(正則表達(dá)式)
- JavaScript RegExp 正則表達(dá)式對(duì)象詳細(xì)說(shuō)明
- js正則表達(dá)式講解之index屬性(RegExp對(duì)象)
- 詳解JavaScript正則表達(dá)式之RegExp對(duì)象
- JavaScript 正則表達(dá)式之RegExp屬性、方法及應(yīng)用分析
- JS正則RegExp.test()使用注意事項(xiàng)(不具有重復(fù)性)
- javascript RegExp 對(duì)象屬性與方法和應(yīng)用
- 注意 JavaScript 中 RegExp 對(duì)象的 test 方法
- javascript類(lèi)型系統(tǒng)_正則表達(dá)式RegExp類(lèi)型詳解
- JavaScript引用類(lèi)型RegExp基本用法詳解
相關(guān)文章
JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07詳解JS如何解決大數(shù)據(jù)下滾動(dòng)頁(yè)面卡頓問(wèn)題
之前遇到不分頁(yè)直接獲取到全部數(shù)據(jù),前端滾動(dòng)查看數(shù)據(jù),頁(yè)面就挺卡頓的,所以這篇文章來(lái)和大家聊聊如何解決這一問(wèn)題,感興趣的小伙伴可以了解下2023-07-07利用jsonp與代理服務(wù)器方案解決跨域問(wèn)題
這篇文章主要給大家介紹了關(guān)于利用jsonp與代理服務(wù)器方案解決跨域問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09小程序選項(xiàng)卡以及swiper套用(跨頁(yè)面)
這篇文章主要為大家詳細(xì)介紹了小程序選項(xiàng)卡以及swiper套用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06小程序點(diǎn)贊收藏功能的實(shí)現(xiàn)代碼示例
這篇文章主要介紹了小程序點(diǎn)贊收藏功能的實(shí)現(xiàn)代碼示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09微信小程序開(kāi)發(fā)中組件的生命周期詳細(xì)介紹
生命周期是指一個(gè)對(duì)象從創(chuàng)建→>運(yùn)行>銷(xiāo)毀的整個(gè)階段,強(qiáng)調(diào)的是一個(gè)時(shí)間段,文中介紹了小程序中組件的生命周期,需要的朋友可以參考下2022-08-08JavaScript組合拼接字符串的效率對(duì)比測(cè)試
這篇文章主要介紹了JavaScript組合拼接字符串的效率對(duì)比測(cè)試,本文測(cè)試了IE6、Firefox、Mozilla、Netscape、Opera等瀏覽器,需要的朋友可以參考下2014-11-11