Home  >  Article  >  Web Front-end  >  Usage of replace() assignment in js

Usage of replace() assignment in js

一个新手
一个新手Original
2017-09-11 09:13:451589browse

<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn