Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to replace all dots in a string using JavaScript?
In this tutorial, we will explore different ways to replace all dots in a string using JavaScript. While it's possible to remove all occurrences of dots manually with a for loop, JavaScript provides built-in methods that make this task much simpler.
The two most popular methods for replacing all dots in a string are:
Using the replaceAll() Method
The replaceAll() method replaces all occurrences of a specified pattern with a replacement string. This is the most straightforward approach for replacing all dots.
Syntax
string.replaceAll(searchValue, replaceValue)
Parameters
searchValue ? The string or pattern to search for. This is the substring that will be replaced.
replaceValue ? The string that will replace each occurrence of the search value.
Example
Here's how to replace all dots with spaces using replaceAll():
<!DOCTYPE html>
<html>
<body>
<script>
var originalString = "how.are.you.today";
var modifiedString = originalString.replaceAll(".", " ");
document.write("Original string: " + originalString);
document.write("<br>Modified string: " + modifiedString);
</script>
</body>
</html>
Original string: how.are.you.today Modified string: how are you today
Using the replace() Method with Regular Expression
The replace() method can also replace all dots when used with a regular expression and the global flag (g). Without the global flag, it would only replace the first occurrence.
Syntax
string.replace(/\./g, replacement)
Example
The regular expression /\./g matches all dots in the string:
<!DOCTYPE html>
<html>
<body>
<script>
var originalString = "how.are.you.today";
var modifiedString = originalString.replace(/\./g, " ");
document.write("Original string: " + originalString);
document.write("<br>Modified string: " + modifiedString);
</script>
</body>
</html>
Original string: how.are.you.today Modified string: how are you today
Comparison
| Method | Browser Support | Syntax Complexity | Performance |
|---|---|---|---|
replaceAll() |
ES2021+ (Modern browsers) | Simple | Good |
replace() with regex |
All browsers | Moderate | Good |
Different Replacement Examples
You can replace dots with any string you want:
<!DOCTYPE html>
<html>
<body>
<script>
var text = "file.name.txt";
// Replace with underscore
var withUnderscore = text.replaceAll(".", "_");
// Replace with dash
var withDash = text.replace(/\./g, "-");
// Remove completely (empty string)
var removed = text.replaceAll(".", "");
document.write("Original: " + text + "<br>");
document.write("With underscore: " + withUnderscore + "<br>");
document.write("With dash: " + withDash + "<br>");
document.write("Removed: " + removed);
</script>
</body>
</html>
Original: file.name.txt With underscore: file_name_txt With dash: file-name-txt Removed: filenametxt
Conclusion
Both replaceAll() and replace() with regex effectively replace all dots in a string. Use replaceAll() for simplicity in modern environments, or replace() with regex for broader browser compatibility.
