192

I have this

 var date = $('#Date').val();

this get the value in the textbox what would look like this

12/31/2009

Now I do this on it

var id = 'c_' + date.replace("/", '');

and the result is

c_1231/2009

It misses the last '/' I don't understand why though.

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • 8
    This is so painful and unnecessary. I wonder what reason there was to implement `replace` like this. I even choose `str.split(search).join(replacement)` over the regexp. – Avatar Dec 18 '17 at 16:35
  • Update: there is a proposal to add `String.prototype.replaceAll` to the standard: https://2ality.com/2019/12/string-prototype-replaceall.html – Cole Tobin Dec 21 '19 at 22:47
  • @ColeTobin no implemented in all major browsers https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll#browser_compatibility – jave.web Mar 26 '22 at 07:40

3 Answers3

307

You need to set the g flag to replace globally:

date.replace(new RegExp("/", "g"), '')
// or
date.replace(/\//g, '')

Otherwise only the first occurrence will be replaced.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
84

Unlike the C#/.NET class library (and most other sensible languages), when you pass a String in as the string-to-match argument to the string.replace method, it doesn't do a string replace. It converts the string to a RegExp and does a regex substitution. As Gumbo explains, a regex substitution requires the g‍lobal flag, which is not on by default, to replace all matches in one go.

If you want a real string-based replace — for example because the match-string is dynamic and might contain characters that have a special meaning in regexen — the JavaScript idiom for that is:

var id= 'c_'+date.split('/').join('');
bobince
  • 528,062
  • 107
  • 651
  • 834
  • 2
    If you wanted to really push this kind of functionality, you might try something like `String.prototype.strReplace = function(needle, replacement) {return this.split(needle).join(replacement||"");};` Then you could `var id = "c_" + date.strReplace("/")` – Patrick Sep 13 '12 at 15:28
  • I like the explanation in this better than any other answers to similar questions. The accepted answer only provides a workaround, not an answer to 'Why' as in the title. – JakeJ Aug 06 '13 at 13:49
  • This only seems to work on the first two instances of my search string... all others are ignored...?? (Does it mater that my search instance has multiple characters? – Danimal111 Jan 14 '15 at 22:16
  • @bobince, this doesn't seem to be the case: "It converts the string to a RegExp and does a regex substitution." Look at these: `"abc".replace("^a", "_")` » `"abc"` and `"abc".replace(new RegExp("^a"), "_")` » `"_bc"` – tomekwi Feb 04 '15 at 18:29
  • 2
    @tomekwi Part of converting a string to a regex would involve escaping special characters. – Ken Wayne VanderLinde Dec 06 '17 at 03:15
12

You can use:

String.prototype.replaceAll = function(search, replace) {
if (replace === undefined) {
    return this.toString();
}
return this.split(search).join(replace);
}