JS正則截取兩個字串之間的字串

來源:互聯網
上載者:User

標籤:進階語言   樣本   regexp   source   .com   pre   class   regex   log   

match方法
var str = "iid0000ffr";var substr = str.match(/id(\S*)ff/);console.log(substr)

返回結果為:["id0000ff", "0000"]

()裡的\S*運算式匹配所有字串

在進階語言裡,我們會用一個叫數量詞的概念:

(?=ff)這表示以ff結尾的前面的字串,但不包括ff
var str = "iid0000ffr";var substr = str.match(/(\S*)(?=ff)/);console.log(substr)

返回數組:["iid0000", "iid0000"]

(iid=?)這表示以iid開頭的字串,但不包括iid
var str = "iid0000ffr";var substr = str.match(/(iid=?)(\S*)/);console.log(substr);

返回數組:["iid0000ffr", "iid", "0000ffr"]

跟第一個樣本一樣擷取前後中間的字串

var str = "iid0000ffr";var substr = str.match(/(iid=?)(\S*)(?=ffr)/);console.log(substr);

返回數組:["iid0000", "iid", "0000"]

 

原文地址http://www.cnblogs.com/dtdxrk/p/4601890.html

JS正則截取兩個字串之間的字串

聯繫我們

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