search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[JS] 尋找字串中所有符合樣式的實例

此篇文章瀏覽量: 635
var searchString = "Now is the time and this is the time and that is the time";
var pattern = /t\w*e/g;
var matchArray;

var str = "";
      
// 使用 RegExp exec 搜尋樣式,假如回傳值不是 null,則處理搜尋結果
while( (matchArray = pattern.exec(searchString)) != null ){
  str += "at " + matchArray.index + " we found " + matchArray[0] + "\n";
}
console.log(str);

以上的輸出結果為:

at 7 we found the
at 11 we found time
at 28 we found the
at 32 we found time
at 49 we found the
at 53 we found time

 

若覺得文章有幫助,請多分享,讓其他同好也能吸收網站技能知識。



熱門推薦

本文由 carlos-studiocom 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦