手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >javascript字符串循环匹配实例分析
javascript字符串循环匹配实例分析
摘要:本文实例讲述了javascript字符串循环匹配的方法。分享给大家供大家参考。具体如下:采用exec和String.match方法,对于ex...

本文实例讲述了javascript字符串循环匹配的方法。分享给大家供大家参考。具体如下:

采用exec和String.match方法,对于exec必须开启全局匹配g标识才能获取所有匹配

// 需要提取这种数据 <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>' + '<td>2012-12-03</td><td>10:16 , 13:22 , 20:05</td></tr><tr>' + '<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标识才会全局匹配,否则只匹配一个 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标识才会全局匹配 var matchedArray = rawData.match(regexp); console.dir(matchedArray); // 方法三 var regexp = /<td>(d{4}-d{2}-d{2})</td><td>(.*?)</td>/; // 不加g标识 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程序设计有所帮助。

【javascript字符串循环匹配实例分析】相关文章:

Javascript节点关系实例分析

JavaScript检查子字符串是否在字符串中的方法

javascript组合使用构造函数模式和原型模式实例

javascript操作ul中li的方法

浅谈javascript中的闭包

Javascript中For In语句用法实例

JavaScript检测字符串中是否含有html标签实现方法

JavaScript的while循环的使用

原生javascript实现解析XML文档与字符串

javascript的事件描述

精品推荐
分类导航