How to Replace all Dots in a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
4 min

banner

# Replace all dots in a String in JavaScript

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.

index.js
const str = 'a.b.c'; const withoutDots = str.replaceAll('.', '-'); console.log(withoutDots); // ๐Ÿ‘‰๏ธ 'a-b-c'

replace all dots in string

The code for this article is available on GitHub

If you want to remove all dots from the string, use an empty string as the replacement.

index.js
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:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.
index.js
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.

index.js
function replaceAllDots(string, replacement) { return string.replaceAll('.', replacement); } // ๐Ÿ‘‡๏ธ bobby-hadz console.log(replaceAllDots('bobby.hadz', '-')); // ๐Ÿ‘‡๏ธ bobby!hadz console.log(replaceAllDots('bobby.hadz', '!'));

defining reusable function

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.

# Replace all dots in a String using String.split()

This is a two-step process:

  1. Use the split() method to split the string into an array.
  2. Use the join() method to join the array into a string with a separator.
index.js
const str = 'a.b.c'; const withoutDots = str.split('.').join('_'); console.log(withoutDots); // ๐Ÿ‘‰๏ธ 'a_b_c'

replace all dots in string using string split

The code for this article is available on GitHub

If you want to remove all dots from the string, use an empty string separator when joining the array.

index.js
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.

index.js
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:

NameDescription
separatorThe pattern describing where each split should occur.
limitAn 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.

index.js
function replaceAllDots(string, replacement) { return string.split('.').join(replacement); } // ๐Ÿ‘‡๏ธ bobby-hadz console.log(replaceAllDots('bobby.hadz', '-')); // ๐Ÿ‘‡๏ธ bobby!hadz console.log(replaceAllDots('bobby.hadz', '!'));

creating reusable function that uses split

The function takes a string and a replacement as parameters and replaces all occurrences of a dot in the string.

# Replace all dots in a String using String.replace()

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

index.js
const str = 'a.b.c'; const withoutDots = str.replace(/\./g, '_'); console.log(withoutDots); // ๐Ÿ‘‰๏ธ 'a_b_c'

replace all dots in string using string replace

The code for this article is available on GitHub

If you want to remove all dots from the string, use an empty string as the replacement string.

index.js
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:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA 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.

index.js
function replaceAllDots(string, replacement) { return string.replace(/\./g, replacement); } // ๐Ÿ‘‡๏ธ bobby-hadz console.log(replaceAllDots('bobby.hadz', '-')); // ๐Ÿ‘‡๏ธ bobby!hadz console.log(replaceAllDots('bobby.hadz', '!'));

defining reusable function to replace all dots in string

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev