Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Here’s my code snippet: 

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

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

It removes only the first occurrence of 'abc'. 

Can anyone tell me how to replace all occurrences of a string in Javascript? 

1 Answer

0 votes
by (19.7k points)

The replaceAll() method in JavaScript replaces all the occurrences of a String and returns it. The original string is left unchanged. 

Here’s an example code implementation : 

const my_string = “So I said yes to Thomas Clinton and later thought that I had said yes to God and later still realized I had said yes only to Thomas Clinton.”

console.log(my_string.replaceAll(‘yes’, ‘no’)); 

The expected output: “So I said no to Thomas Clinton and later thought that I had said no to God and later still realized I had said no only to Thomas Clinton.”

If you want to learn more about Javathen go through this Java tutorial by Intellipaat for more insights.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked May 1, 2021 in Java by sheela_singh (9.5k points)

Browse Categories

...