<script>var str = "a1ba2b";var reg = /a.b/g; str = str.replace(reg,function(a,b){ console.log(a); console.log(b); return b == 0 ? a.replace("a","0") : a.replace("b","3"); }); console.log(str);/* 输出结果为: a1b 0//第一次匹配到a1b,将a置为0. a2b 3//第二次匹配到a2b,将b置为3. 01ba23//返回经过修改后的字符串 *//* function(a,b,c)一共可以传入3个参数,第一个为匹配的字符串,第二个为匹配字符串的起始位置, 第三个为调用replace方法的字符串本身。可以缺省c或b、c。 */</script>
The above is the detailed content of Usage of replace() assignment in js. For more information, please follow other related articles on the PHP Chinese website!