198

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.

5
  • 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. Commented Dec 18, 2017 at 16:35
  • Update: there is a proposal to add String.prototype.replaceAll to the standard: 2ality.com/2019/12/string-prototype-replaceall.html Commented Dec 21, 2019 at 22:47
  • @ColeTobin no implemented in all major browsers developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 26, 2022 at 7:40
  • 3
    @jave.web now implemented in all major browsers. This is so simple : just date.replaceAll('/','') Commented Sep 5, 2023 at 15:51
  • @matanh that's great news! even included in caniuse.com/?search=replaceAll :-) Commented Sep 5, 2023 at 18:38

3 Answers 3

314

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Why difference then C# replace. Thought it would replace all occurrences by default. But why did it take 2 slashes away if it is only first occurrence?
@chobo2 it didn't take away two slashes. There were only two to begin with, and it removed the first one.
@chobo2: Well, JavaScript is not C#. And 12/31/2009 does only contain two slashes.
Perhaps the most informative comment I've seen regarding Regular Expressions!
85

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('');

5 Comments

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("/")
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.
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?
@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 Part of converting a string to a regex would involve escaping special characters.
12

You can use:

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

1 Comment

Now String has an officially supported replaceAll function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.