tags:

views:

484

answers:

5

Say I have this string:

Test abc test test abc test test
test abc test test abc

Doing

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

Seems only to remove the first occurrence of abc in the string above. How can I replace ALL occurrences of it?

+9  A: 
str = str.replace(/abc/g, '');

In response to comment:

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(find, replace, str) {
  return str.replace(new RegExp(find, 'g'), replace);
}
Sean Bright
How would I write this if abc was in a variable instead of the direct string? E.g `str=str.replace(myStr,'');`
Click Upvote
I've updated based on your question.
Sean Bright
what cant regex do?
Petey B
Nothing. I am writing a time capsule in RegEx right now.
Josh Stodola
Regular expressions are the only way to accomplish this "out of the box" without implementing your own 'replaceAll' method. I'm not suggesting their use just for the sake of using them.
Sean Bright
This is my own function from this comment: `function replaceAll(find, replace,str){ var re = new RegExp(find, 'g'); str = str.replace(re, replace); return str;}`
Click Upvote
+4  A: 
str = str.replace(/abc/g, '');

Or try the replaceAll function from here:

http://stackoverflow.com/questions/1137436/useful-javascript-methods-that-extends-built-in-objects/1137579#1137579

str = str.replaceAll('abc', ''); OR

var search = 'abc';
str = str.replaceAll(search, '');

EDIT: Clarification about replaceAll availability

The 'replaceAll' method is added to String's prototype. This means it will be available for all string objects/literals.

E.g.

var output = "test this".replaceAll('this', 'that');  //output is 'test that'.
output = output.replaceAll('that', 'this'); //output is 'test this'
SolutionYogi
Can you rewrite the replaceAll() function there for those not using prototype?
Click Upvote
@Click Upvote....you are using prototype, it's part of all JS objects. I think you are thinking of prototype.js the JS library.
seth
seth, a little correction. If you add method to a prototype, it is available to all objects of that type. The replceAll method was added to String prototype and it should work for all string objects.
SolutionYogi
@solutionyogi -- Yep, I've used prototype (correctly) before. I was just addressing the OP's comment about "not using prototype" which I assumed to mean Prototype.js (perhaps incorrectly?). I should have said "a prototype" as I was trying to say JavaScript objects have a prototype. Thus, the OP was already 'using prototype,' albeit in an "indirect" way. Indirect might be the wrong term to use here but I'm tired so mea culpa.
seth
A: 

use a regex:

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

Donnie DeBoer
A: 

You could try using your existing code, then throw it in a while loop... Not sure what this will do for performance, but it will definitely get the job done!

var exists = false;
if (str.indexOf('abc') > 0)
    exists = true; // does 'abc' exist in my string?

while (exists) // replace 'abc' as long as it exists
{
    str = str.replace('abc', '');

    if (str.indexOf('abc') > 0)
        exists = true;
    else
        exists = false;
}
mbmccormick
-1 This doesnt work, and it's way too much code.
Josh Stodola
+1  A: 

As an alternative to regular expressions for a simple literal string, you could use

str = "Test abc test test abc test...".split("abc").join("");

The general pattern is

string.split(search).join(replacement)

I would wrap it in a function so it's more obvious what's going on.

Matthew Crumley