javascript字串迴圈匹配執行個體分析,javascript執行個體分析

來源:互聯網
上載者:User

javascript字串迴圈匹配執行個體分析,javascript執行個體分析

本文執行個體講述了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 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標識才會全域匹配,否則只匹配一個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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.