Replace a comma with a Dot using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Replace a comma with a Dot using JavaScript #

Use the replaceAll() method to replace all commas with dots, e.g. str.replaceAll(',', '.'). The replaceAll method will return a new string where all commas are replaced with dots.

index.js
const str1 = '123,456,789'; // ✅ Replace ALL Commas with Dots const replaced1 = str1.replaceAll(',', '.'); console.log(replaced1); // 👉️ 123.456.789 // ✅ Replace FIRST comma with Dot const replaced2 = str1.replace(/,/, '.'); console.log(replaced2); // 👉️ 123.456,789 // ✅ Replace ALL commas with Dot using Regex const replaced3 = str1.replace(/,/g, '.'); console.log(replaced3); // 👉️ 123.456.789

The first example uses the String.replaceAll method to replace all commas in a string with dots.

The 2 arguments we passed to the replaceAll method are:

  1. the substring we want to match in the string
  2. the replacement for each match

The replaceAll method returns a new string where all of the matches are replaced by the provided replacement.

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

If you only need to replace the first occurrence of a comma with a dot, use the String.replace method.

Use the String.replace() method to replace the first occurrence of a comma with a dot, e.g. const replaced = str1.replace(/,/, '.');. The replace() method will return a new string where the first occurrence of a comma is replaced with a dot.

index.js
const str1 = '123,456,789'; // ✅ Replace FIRST comma with Dot const replaced2 = str1.replace(/,/, '.'); console.log(replaced2); // 👉️ 123.456,789

The first argument we passed to the replace method is a regular expression.

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

All we try to match in our regular expression is a single comma.

By default, the replace method replaces the first occurrence of the matched regular expression with the provided replacement string.

An alternative to using the replaceAll method is to use the replace method with the g (global) flag.

Use the replace() method to replace all commas with dots, e.g. const replaced = str1.replace(/,/g, '.');. The replace method will return a new string with all commas replaced by dots.

index.js
const str1 = '123,456,789'; // ✅ Replace ALL commas with Dot using Regex const replaced3 = str1.replace(/,/g, '.'); console.log(replaced3); // 👉️ 123.456.789
We used the g (global) flag because we want to match all occurrences of a comma in the string, and not just the first occurrence.

If you ever need help reading a regular expression, check out this regex cheatsheet from MDN.

Which approach you pick is a matter of personal preference. I'd go with the replaceAll() method to replace all occurrences of a comma with a dot, and the replace() method if I only need to replace the first occurrence.

Further Reading #

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.