string.replace() 全文替换

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");

方可实现全局替换

posted @ 2020-10-12 15:00  君莫笑·秋  阅读(889)  评论(0编辑  收藏  举报