JS(javascript)Regex整理文章(總結)

來源:互聯網
上載者:User

Regex很強大,必會。

每種語言Regex都類似,學會一種,其他的上手很快的、

JSRegex精彩博文:

1 JavaScript RegExp 對象參考手冊 W3C    http://www.w3school.com.cn/js/jsref_obj_regexp.asp

2  JavaScript使用Regex http://www.yaosansi.com/post/745.html

3  JS的Regex  http://www.iteye.com/topic/30728

4 Javascript經典Regex http://www.dedecms.com/html/xitongyanshi/20070423/38637.html

5 溫故知新 javascript Regex http://www.cnblogs.com/libinqq/archive/2008/07/31/1257699.html

JS進階替換功能

簡單替換就不說了,比如這樣一個字串

<img type="inputline" style="display:inline;" src="/OJ/GetImage?type=inputline&num=2" /><img type="inputline" style="display:inline;" src="/OJ/GetImage?type=inputline&num=31" /><img type="inputline" style="display:inline;" src="/OJ/GetImage?type=inputline&num=1" />

注意到其中num=2,num=3,num=1我們如何替換這num=後面跟的數字呢?

var regex = /\<img type=['"]([a-zA-z]+)['"][^>]+num=(\d)[^>]* \/\>/g;//提取出<img ... />,寫複雜了,可以把num=後面的數字也可以提取出來

var result = "";
var data = "上面那串";
var i=1;
result = data.replace(regex, function(sMatch){//sMatch就是每次提取出來的一個<img ... />
    return sMatch.replace(/num=\d/, "num="+i++);
});
alert(result);

相關文章

聯繫我們

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