Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Web Technology by (47.6k points)

I have this string:

"Test abc test test abc test test test abc test test abc"

Doing:

str = str.replace('abc', '');

seems to only remove the first occurrence of abc in the string above.

How can I replace all occurrences of it?

1 Answer

0 votes
by (106k points)

You can use split and join method for replacing all occurrences of a string below is the code for the same:-

string="Test abc test test abc test test test abc test test abc"

String.prototype.replaceAll = function(search, replacement) { var target = this; 

return target.split(search).join(replacement); 

};

Related questions

0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...