var test = 'abcdeabcdeabcdddd';
console.log(test.replace('a', '6'));//6bcdeabcdeabcdddd
console.log(test.replace(/a/g, '6'));//6bcde6bcde6bcdddd

匹配全文,应使用正则表达式

‘a’

更换为

/a/g

/g表示全局,

如果需要替换的‘a’为变量,则应使用

string.replace(new RegExp(key,'g'),"b");

方可实现全局替换

相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-10-05
  • 2021-12-26
  • 2021-11-20
  • 2022-01-15
猜你喜欢
  • 2022-12-23
  • 2022-01-07
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案