52

Possible Duplicate:
How to replace all points in a string in JavaScript

I am trying to remove '.'(dot) symbol from my string. and The code that Ive used is

checkedNew = checked.replace('.', "");

Bt when I try to alert the value of checkedNew, for example if the checkedNew has original value U.S. Marshal, the output that I get is US. Marshal, it will not remove the second dot in that string. How do remove all dot symbols?

Community
  • 1
  • 1
user1371896
  • 2,192
  • 7
  • 23
  • 32

3 Answers3

118

Split the string on all the .'s and then join it again with empty spaces, like this:

checkedNew = checked.split('.').join("");
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
64

You need to perform a global replacement as, by default, replace only performs one replacement. In theory you can pass an instruction to be global as the third argument, but that has some compatibility issues. Use a regular expression instead.

checkedNew = checked.replace(/\./g, "");
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
23

replace will only replace the first occurance. To get around this, use a regex with the global option turned on:

checked.replace(/\./g, '');
jbabey
  • 45,965
  • 12
  • 71
  • 94