2022-07-26 js replace函数匹配所有项,替换成指定字符

需求:如题。

方案:

let a = '1,22,333,4444,55555'; console.log(a.replace(/,/g,'==')); // 1==22==333==4444==55555

注:如果你只是匹配一个字符串(默认第一个),那么写法是

a.replace(',','==')

附:

  1. 函数结构: stringObject.replace(regexp/substr,replacement)
  2. stringObject:字符串
  3. replace:函数名
  4. regexp/substr,:规定子字符串或要替换的模式的 RegExp 对象 // 来源:https://m.php.cn/article/481572.html
  5. replacement:要替换的字符
posted @ 2022-07-26 15:56  叶乘风  阅读(128)  评论(0编辑  收藏  举报