javascript字符串循環(huán)匹配實例分析
更新時間:2015年07月17日 15:35:50 作者:優(yōu)雅先生
這篇文章主要介紹了javascript字符串循環(huán)匹配,實例分析三種常用的字符串循環(huán)匹配的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了javascript字符串循環(huán)匹配的方法。分享給大家供大家參考。具體如下:
采用exec和String.match方法,對于exec必須開啟全局匹配g標(biāo)識才能獲取所有匹配
// 需要提取這種數(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">簽到簽退時間</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)識才會全局匹配,否則只匹配一個 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)識才會全局匹配 var matchedArray = rawData.match(regexp); console.dir(matchedArray); // 方法三 var regexp = /<td>(\d{4}-\d{2}-\d{2})<\/td><td>(.*?)<\/td>/; // 不加g標(biāo)識 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);
希望本文所述對大家的javascript程序設(shè)計有所幫助。
相關(guān)文章
javascript獲取不重復(fù)的隨機數(shù)的方法比較
js永不重復(fù)隨機數(shù)實現(xiàn)代碼比較2008-09-09javascript數(shù)組includes、reduce的基本使用
這篇文章主要給大家介紹了關(guān)于javascript數(shù)組includes、reduce的基本使用方法,includes方法是用于檢查特定元素是包含在數(shù)組還是字符串中的方法,而reduce用法則有很多,需要的朋友可以參考下2021-07-07