javascript 正則匹配 提取全部 preg_match_all matchAll方法,javascript正則匹配

來源:互聯網
上載者:User

javascript 正則匹配 提取全部 preg_match_all matchAll方法,javascript正則匹配

javascript 提取所有的的方法,javascript中沒有matchAll這個方法。

用while來實作類別似 PHP 中的preg_match_all() :(by default7#zbphp.com)

<!DOCTYPE html><html><head><title></title></head><body><select name="AgentIdP" id="AgentIdP" onchange="selectNull('AgentIdI333');selectNull('GameId333');"></select><script type="text/javascript">var html = document.getElementById('AgentIdP').getAttribute('onchange');var exp = /selectNull\([\'"]{1}([\w-]+)[\'"]{1}\)/ig;var result ;while( (result = exp.exec(html))!= null){console.log(result);}console.log(html);</script></body></html>




preg_match_all 正則怎匹配 css="procon" 的div下的所有內容,

不好匹配,因為你的div不確定是最裡層
 
怎運用PHP函數preg_match_all測試Regex效果

  PHP函數preg_match_all代碼執行個體:php自學網2< /div< div id="biuuu_3"php自學網3< /div';  PHP函數preg_match_all執行個體要求:分別將每一個DIV元素的ID和內容取出,如biuuu,biuuu_2,biuuu_3,php自學網,php自學網2和php自學網3(一些常用的抓站方法就是這樣匹配的)  分析:字串是一個簡單的HTML元素,每一個DIV元素對應該一個ID和內容,並且是獨立的,首先考慮如何取出一個DIV內的ID值和內容, 如:php自學網,然後匹配其它類似的元素。一個DIV中需要取出兩個值,也就是兩個匹配的運算式,第一個運算式用於匹配ID值(biuuu),第二個表 達式用於匹配ID的內容(php自學網),Regex常用的運算式使用小括弧,那麼前面的元素將會變成如下形式:  < div id="(biuuu)"(php自學網)< /div < div id="(運算式1)"(運算式2)< /div  運算式1:[a-zA-Z0-9_]+ (表示匹配大小寫字母,數字和底線)  運算式2:[^<]+ (表示不匹配<和字元)  這樣,PHP函數preg_match_all需要匹配的子運算式就實現了,但是還要需要匹配一個的運算式,方法如下:  運算式:/ '"(運算式1)"'(運算式2)/  注意其中的雙引號"和/需要使用逸出字元轉義,然後把前面兩個運算式放進去,如下:  '"([a-z0-9_]+)"'/< div id="([a-z0-9_]+)"([^<]+)< /div/  這樣就實現一個匹配每一個DIV元素ID值和內容的Regex,然後使用preg_match_all函數測試如下:  $html = '< div id="biuuu"php自學網< /div< div id="biuuu_2"php自學網2< /div< div id="biuuu_3"php自學網3< /div'; preg_match_all('/< divsiddivsid="([a-z0-9_]+)"([^<]+)< /div/',$html,$result); var_dump($result);  PHP函數preg_match_all樣本結果:  array(3) { [0]= array(3) { [0]= string(30) "php自學網" [1]= string(33) "php自學網2" [2]= string(33) "php自學網3" } [1]= array(3) { [0]= string(5) "biuuu" [1]= string(7) "biuuu_2" [2]= string(7) "biuuu_3&......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.