OneCompiler

[Javascript] How can I replace all the occurrences of a given substring in a long string?

I want to replace all the occurrences of a given substring in a long string, How can I do that in JavaScript?

1 Answer

4 years ago by

Let us consider the below string, where you want to replace 123 with xyz.

let str = 'abc 123 abc 123 abc 123 abc 123';

console.log(str.replace(/123/g,'xyz'));

Run here https://onecompiler.com/javascript/3xns8rbbm

4 years ago by Meera