Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all occurrences of a string in JavaScript

I have this string in my JavaScript code:

"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?

like image 741
Ali Avatar asked Jul 17 '09 17:07

Ali


People also ask

How do you replace all occurrences of a string?

replaceAll = String. prototype. replaceAll || function(string, replaced) { return this. replace(new RegExp(string, 'g'), replaced); };

How do you remove all occurrences of a character from a string in JavaScript?

Delete all occurrences of a character in javascript string using replaceAll() The replaceAll() method in javascript replaces all the occurrences of a particular character or string in the calling string. The first argument: is the character or the string to be searched within the calling string and replaced.

Does JavaScript have replaceAll?

replaceAll() The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match.

How will you replace all occurrences of old substring in string with new string?

Python String | replace() replace() is an inbuilt function in the Python programming language that returns a copy of the string where all occurrences of a substring are replaced with another substring. Parameters : old – old substring you want to replace. new – new substring which would replace the old substring.


1 Answers

As of August 2020: Modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 language specification.


For older/legacy browsers:

function escapeRegExp(string) {   return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string }  function replaceAll(str, find, replace) {   return str.replace(new RegExp(escapeRegExp(find), 'g'), replace); } 

Here is how this answer evolved:

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

In response to comment "what's if 'abc' is passed as a variable?":

var find = 'abc'; var re = new RegExp(find, 'g');  str = str.replace(re, ''); 

In response to Click Upvote's comment, you could simplify it even more:

function replaceAll(str, find, replace) {   return str.replace(new RegExp(find, 'g'), replace); } 

Note: Regular expressions contain special (meta) characters, and as such it is dangerous to blindly pass an argument in the find function above without pre-processing it to escape those characters. This is covered in the Mozilla Developer Network's JavaScript Guide on Regular Expressions, where they present the following utility function (which has changed at least twice since this answer was originally written, so make sure to check the MDN site for potential updates):

function escapeRegExp(string) {   return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } 

So in order to make the replaceAll() function above safer, it could be modified to the following if you also include escapeRegExp:

function replaceAll(str, find, replace) {   return str.replace(new RegExp(escapeRegExp(find), 'g'), replace); } 
like image 68
Sean Bright Avatar answered Sep 22 '22 17:09

Sean Bright