javascript字符串循環(huán)匹配實(shí)例分析
本文實(shí)例講述了javascript字符串循環(huán)匹配的方法。分享給大家供大家參考。具體如下:
采用exec和String.match方法,對(duì)于exec必須開啟全局匹配g標(biāo)識(shí)才能獲取所有匹配
// 需要提取這種數(shù)據(jù) <td>2012-12-17</td><td>11:02 , 12:25 , 13:22 , 15:06 , 15:12 , 19:22 , 23:47</td> var rawData = '<table><th align="left" scope="col">日期</th><th align="left" scope="col">簽到簽退時(shí)間</th></tr><tr class="GridViewRowStyle" style="height:20px;">' + '<td>2012-12-03</td><td>10:16 , 13:22 , 20:05</td></tr><tr class="GridViewRowStyle" style="height:20px;">' + '<td>2012-12-04</td><td>11:16 , 14:22 , 21:05</td></tr><table>'; // 方法一 var regexp = /<td>(\d{4}-\d{2}-\d{2})<\/td><td>(.*?)<\/td>/g; // 加上g標(biāo)識(shí)才會(huì)全局匹配,否則只匹配一個(gè) var matchedArray = regexp.exec(rawData); while(matchedArray != null) { console.dir(matchedArray); matchedArray = regexp.exec(rawData); } // 方法二 var regexp = /<td>(\d{4}-\d{2}-\d{2})<\/td><td>(.*?)<\/td>/g; // 加上g標(biāo)識(shí)才會(huì)全局匹配 var matchedArray = rawData.match(regexp); console.dir(matchedArray); // 方法三 var regexp = /<td>(\d{4}-\d{2}-\d{2})<\/td><td>(.*?)<\/td>/; // 不加g標(biāo)識(shí) var matchedArray = rawData.match(regexp); console.dir(matchedArray); console.log(matchedArray.index); while(matchedArray != null) { rawData = rawData.substr(matchedArray.index + matchedArray[0].length); matchedArray = rawData.match(regexp); } console.dir(matchedArray);
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
javascript獲取不重復(fù)的隨機(jī)數(shù)的方法比較
js永不重復(fù)隨機(jī)數(shù)實(shí)現(xiàn)代碼比較2008-09-09javascript數(shù)組includes、reduce的基本使用
這篇文章主要給大家介紹了關(guān)于javascript數(shù)組includes、reduce的基本使用方法,includes方法是用于檢查特定元素是包含在數(shù)組還是字符串中的方法,而reduce用法則有很多,需要的朋友可以參考下2021-07-07微信小程序調(diào)用攝像頭實(shí)現(xiàn)拍照功能
這篇文章主要為大家詳細(xì)介紹了微信小程序調(diào)用攝像頭實(shí)現(xiàn)拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07解決eclipse中沒(méi)有js代碼提示的問(wèn)題
今天小編就為大家分享一篇解決eclipse中沒(méi)有js代碼提示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10如何基于javascript實(shí)現(xiàn)貪吃蛇游戲
這篇文章主要介紹了如何基于javascript實(shí)現(xiàn)貪吃蛇游戲,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02針對(duì)BootStrap中tabs控件的美化和完善(推薦)
這篇文章主要介紹了針對(duì)BootStrap中tabs控件的美化和完善的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-07-07