76

original string is "a,d,k" I want to remove all , and make it to "adk".

I tried code below but it doesn't work.

"a,d,k".replace(/,/,"")
2
  • 1
    "Doesn't work"? Please elaborate. What "doesn't work" about it? Commented May 26, 2009 at 1:49
  • 2
    I searched for the same questions. thnaks for saving my time Commented Sep 4, 2012 at 12:14

6 Answers 6

126

You aren't assigning the result of the replace method back to your variable. When you call replace, it returns a new string without modifying the old one.

For example, load this into your favorite browser:

<html><head></head><body>
    <script type="text/javascript">
        var str1 = "a,d,k";
        str1.replace(/\,/g,"");
        var str2 = str1.replace(/\,/g,"");
        alert (str1);
        alert (str2);
    </script>
</body></html>

In this case, str1 will still be "a,d,k" and str2 will be "adk".

If you want to change str1, you should be doing:

var str1 = "a,d,k";
str1 = str1.replace (/,/g, "");
Sign up to request clarification or add additional context in comments.

2 Comments

+1, but you can just do var str2; str2 = str.replace(...). I've expanded on the answer to (hopefully) improve it.
If you want to do it without a regex str.split(",").join("")
42

Use String.replace(), e.g.

var str = "a,d,k";
str = str.replace( /,/g, "" );

Note the g (global) flag on the regular expression, which matches all instances of ",".

Comments

7

If U want to delete more than one characters, say comma and dots you can write

<script type="text/javascript">
  var mystring = "It,is,a,test.string,of.mine" 
  mystring = mystring.replace(/[,.]/g , ''); 
  alert( mystring);
</script>

Comments

5

You can try something like:

var str = "a,d,k";
str.replace(/,/g, "");

Comments

-2

<script type="text/javascript">var s = '/Controller/Action#11112';if(typeof s == 'string' && /\?*/.test(s)){s = s.replace(/\#.*/gi,'');}document.write(s);</script>

It's more common answer. And can be use with s= document.location.href;

Comments

-3

If you need a number greater than 999,999.00 you will have a problem.
These are only good for numbers less than 1 million, 1,000,000.
They only remove 1 or 2 commas.

Here the script that can remove up to 12 commas:

function uncomma(x) {
  var string1 = x;
  for (y = 0; y < 12; y++) {
    string1 = string1.replace(/\,/g, '');
  }
  return string1;
}

Modify that for loop if you need bigger numbers.

3 Comments

The g in your regex means global and will remove all the commas from the string on the first pass the other 11 loops do nothing. Also you don't need to escape the comma character in the literal. I would advise not being so hostile and critical of other people's answers.
Strange that it works for me with 11 loops, and not with 1 pass.
Also I was not being "hostile" or "critical". Just informing that the above example is flawed and does not work for numbers greater that 1 million. I needed the code, tested it, it failed, and I have posted my solution which worked only with 11 loops, did not work with 1. Can you tell me why that is? It goes against what you are saying. So I am unsure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.