1. 程式人生 > >Js 利用正則表達式和replace函數獲取string中所有被匹配到的文本

Js 利用正則表達式和replace函數獲取string中所有被匹配到的文本

利用 str 正則表達 mat 則表達式 ole 括號 替換 字符串

js的replace函數除了替換文本以外還有獲取所有被正則表達式匹配到的文本的功能。這裏以一個簡單的案例來作為演示。

利用正則查找出所有被兩個花括號包裹的字符串:

var str = <div class="item">{{test}}{{aaa}}{{bbb}}</div>
str.replace(reg,function (match,param,offset,string) {
        console.log(match,param,offset,string);
})

Js 利用正則表達式和replace函數獲取string中所有被匹配到的文本