2022-07-26 js replace函数匹配所有项,替换成指定字符
需求:如题。
方案:
let a = '1,22,333,4444,55555'; console.log(a.replace(/,/g,'==')); // 1==22==333==4444==55555
注:如果你只是匹配一个字符串(默认第一个),那么写法是
a.replace(',','==')
附:
- 函数结构: stringObject.replace(regexp/substr,replacement)
- stringObject:字符串
- replace:函数名
- regexp/substr,:规定子字符串或要替换的模式的 RegExp 对象 // 来源:https://m.php.cn/article/481572.html
- replacement:要替换的字符