Last updated: Mar 1, 2024
Reading timeยท4 min

Use the String.replaceAll() method to replace all dots in a string, e.g.
str.replaceAll('.', '-');.
The replaceAll method returns a new string with all matches replaced by the
provided replacement.
const str = 'a.b.c'; const withoutDots = str.replaceAll('.', '-'); console.log(withoutDots); // ๐๏ธ 'a-b-c'

If you want to remove all dots from the string, use an empty string as the replacement.
const str = 'a.b.c'; const withoutDots = str.replaceAll('.', ''); console.log(withoutDots); // ๐๏ธ 'abc'
The String.replaceAll() method returns a new string with all matches of a pattern replaced by the provided replacement.
The method takes the following parameters:
| Name | Description |
|---|---|
| pattern | The pattern to look for in the string. Can be a string or a regular expression. |
| replacement | A string used to replace the substring match by the supplied pattern. |
const str = 'a.b.c'; const withoutDots = str.replaceAll('.', '-'); console.log(withoutDots); // ๐๏ธ 'a-b-c'
In the example, we replace all occurrences of a dot with a hyphen.
The String.replaceAll() method returns a new string with the matches of the
pattern replaced. The method doesn't change the original string.
Strings are immutable in JavaScript.
If you have to do this often, define a reusable function.
function replaceAllDots(string, replacement) { return string.replaceAll('.', replacement); } // ๐๏ธ bobby-hadz console.log(replaceAllDots('bobby.hadz', '-')); // ๐๏ธ bobby!hadz console.log(replaceAllDots('bobby.hadz', '!'));

We defined a function that takes a string and a replacement as parameters and replaces all occurrences of a dot in the string.
Alternatively, you can use the String.split() and Array.join() methods.
String.split()This is a two-step process:
split() method to split the string into an array.join() method to join the array into a string with a separator.const str = 'a.b.c'; const withoutDots = str.split('.').join('_'); console.log(withoutDots); // ๐๏ธ 'a_b_c'

If you want to remove all dots from the string, use an empty string separator when joining the array.
const str = 'a.b.c'; const withoutDots = str.split('.').join(''); console.log(withoutDots); // ๐๏ธ 'abc'
We used the String.split() method to split the string on each dot.
const str = 'a.b.c'; const split = str.split('.'); console.log(split); // ๐๏ธ ['a', 'b', 'c']
The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.
The String.split() method takes the following 2 parameters:
| Name | Description |
|---|---|
separator | The pattern describing where each split should occur. |
limit | An integer used to specify a limit on the number of substrings to be included in the array. |
The last step is to use the Array.join() method to join all array elements with a separator (replacement for each dot).
If a value for the separator argument is omitted, the array elements are
joined with a comma ,.
If the separator argument is set to an empty string, the array elements are
joined without any characters in between them.
You can create a reusable function if you have to do this often.
function replaceAllDots(string, replacement) { return string.split('.').join(replacement); } // ๐๏ธ bobby-hadz console.log(replaceAllDots('bobby.hadz', '-')); // ๐๏ธ bobby!hadz console.log(replaceAllDots('bobby.hadz', '!'));

The function takes a string and a replacement as parameters and replaces all occurrences of a dot in the string.
String.replace()Alternatively, you can use the String.replace() method.
const str = 'a.b.c'; const withoutDots = str.replace(/\./g, '_'); console.log(withoutDots); // ๐๏ธ 'a_b_c'

If you want to remove all dots from the string, use an empty string as the replacement string.
const str = 'a.b.c'; const withoutDots = str.replace(/\./g, ''); console.log(withoutDots); // ๐๏ธ 'abc'
The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.
The method takes the following parameters:
| Name | Description |
|---|---|
| pattern | The pattern to look for in the string. Can be a string or a regular expression. |
| replacement | A string used to replace the substring match by the supplied pattern. |
The String.replace() method returns a new string with the matches of the
pattern replaced. The method doesn't change the original string.
The \ character, before the dot, is used to escape special characters.
The dot . character has a special meaning in
regular expressions
and matches one or more characters of any type.
We want the dot to be treated as a literal character, so we escape it.
We used the g (global) flag because we want to match all occurrences of the
dot . in the string, and not just the first occurrence.
If you have to do this often, extract the logic into a reusable function.
function replaceAllDots(string, replacement) { return string.replace(/\./g, replacement); } // ๐๏ธ bobby-hadz console.log(replaceAllDots('bobby.hadz', '-')); // ๐๏ธ bobby!hadz console.log(replaceAllDots('bobby.hadz', '!'));

The function takes a string and a replacement as parameters and replaces all occurrences of a dot in the string.
Which approach you pick is a matter of personal preference. I'd use the
String.replaceAll() method as I find it quite direct and intuitive.
You can learn more about the related topics by checking out the following tutorials: