Hướng dẫn javascript string remove dots

Remove all dots from a String #

To remove all dots from a string:

  1. Call the replaceAll method, passing it a dot as the first parameter and an empty string as the second - str.replaceAll('.', '').
  2. The replaceAll method returns a new string with all matches replaced by the provided replacement.

Copied!

const str = 'a.b.c.'; const dotsRemoved = str.replaceAll('.', ''); console.log(dotsRemoved); // 👉️ 'a_b_c'

We passed the following parameters to the String.replaceAll method:

  1. The character we want to replace in the string>
  2. The replacement for each match.

We used an empty string for the replacement because we want to remove all dots from the string.

The replaceAll() method doesn't change the contents of the original string, it returns a new string with all matches replaced.

Alternatively, you can use the String.replace() method.

Remove all dots from a String using String.replace() #

Use the String.replace() method to remove all dots from a string, e.g. const dotsRemoved = str.replace(/\./g, '');. The replace() method will remove all dots from the string by replacing them with empty strings.

Copied!

const str = 'a.b.c.'; const dotsRemoved = str.replace(/\./g, ''); console.log(dotsRemoved); // 👉️ 'abc'

We passed the following parameters to the String.replace method:

  1. A regular expression that matches all dots in the string.
  2. The replacement for each match.

The forward slashes / / mark the beginning and end of the regular expression.

We prefixed the dot with a backslash to escape it.

The dot . has a special meaning in regular expressions, but we need to match a literal dot ..

We used the g (global) flag to match all occurrences of dots in the string and not just the first occurrence.

By replacing all dots with an empty string, we remove all dots from the string.

The replace method doesn't change the original string, it returns a new string with the matches replaced. Strings are immutable in JavaScript.

Which approach you pick is a matter of personal preference. I'd go with the replaceAll() method to avoid using a regular expression.

Further Reading #

  • Remove Special Characters from a String in JavaScript
  • Remove all Spaces from a String in JavaScript
  • Remove all Numbers from a String in JavaScript
  • Remove the leading Spaces from a String in JavaScript
  • Remove Trailing Spaces from a String in JavaScript
  • Remove the Leading and Trailing Comma from a String in JS
  • Remove all Commas from a String in JavaScript

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?

asked May 14, 2012 at 13:38

user1371896user1371896

2,0847 gold badges23 silver badges32 bronze badges

0

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

checkedNew = checked.split('.').join("");

answered May 14, 2012 at 13:39

Hướng dẫn javascript string remove dots

Elliot BonnevilleElliot Bonneville

49.9k23 gold badges93 silver badges123 bronze badges

6

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, "");

answered May 14, 2012 at 13:40

QuentinQuentin

873k121 gold badges1170 silver badges1285 bronze badges

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

checked.replace(/\./g, '');

answered May 14, 2012 at 13:41

Not the answer you're looking for? Browse other questions tagged javascript or ask your own question.