Q: How to replace all occurrences of a string in JavaScript without using regex?

Javascript replaceAll split join

Answer:

JavaScript doesn't have a built-in replaceAll method to replace all occurrences of a string. It is recommended to use regular expressions to replace all occurrences of a string. A quick hack to replace all occurrence of a string in JavaScript without using regex is to use split and join method.

For example:

'hello, world'.split('world').join('john');

Will output:

"hello, john"

    No comments found for this tutorial, be the first to leave a comment!

Answered by
Double

Last updated on
Dec 28, 2017

Share