How to replace several words in javascript

前端 未结 6 776
心在旅途
心在旅途 2020-12-06 07:43

I wanna replace several words in a text using replace() in javascript, how can I do that?

For example, if I wanna replace, \'Dave Chambers,

6条回答
  •  独厮守ぢ
    2020-12-06 08:09

    You'll want to use regular expressions if you want to ignore case:

    var str = sometext.innerHTML;
    str.replace(/Dave Chambers/ig, 'Jackie Chan')
       .replace(/David Chambers/ig, 'Jackie Chan')
       .replace(/Will Smith/ig, 'Jackie Chan');
    

    You can do it all in one statement like above and that's how I would prefer as it's more readable.

提交回复
热议问题