Home > Web Front-end > JS Tutorial > body text

Usage of replace() assignment in js

一个新手
Release: 2017-09-11 09:13:45
Original
1561 people have browsed it

<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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!