How to replace all dots in a string using JavaScript?



In this tutorial, we will explore the different ways by which we can replace all dots in a string using JavaScript. It is possible to remove all occurrences of dots in a string manually by running a for loop, but JavaScript provides many functions by which we can accomplish the same task very easily. This is precisely what we will discuss in this article.

The two most popular methods by which we can replace all dots in a string using JavaScript are −

Using the replaceAll() method in JavaScript

JavaScript strings have a method called replaceAll(), to which we can pass two parameters, namely pattern and replacement, and the method will replace all the occurrences of the pattern string with the replacement string provided. To be precise, using this method we can replace all occurrences of a particular pattern in a string with whatever we want. Hence, by using this method we can replace the dots in our string with an empty string, thus removing them.

Let us look at the syntax and parameters of the replaceAll() method in JavaScript.

Syntax and Parameters

The syntax of the replaceAll() method is −

s.replaceAll(pattern, replacement);

Here s is the string in which we want to replace some pattern. Let us look at both the parameters in detail −

  • pattern − This refers to the sub string of the string s which is to be converted into something else, the sub string that is to be replaced. This parameter can’t only be a string but can also be a regular expression that denotes the pattern to be found and replaced in string s. Naturally, this is a required parameter.

  • replacement − This denotes the string which will take the place of the pattern in the string s. This is what every occurrence of the pattern will be replaced with. Note that this parameter can also be a function that returns the string which will be used as a replacement for each occurrence of the pattern in s in case different replacements are needed for different occurrences.

Example

In the below example code, we used replaceAll() method to remove all ‘.’ and have them changed to ‘ ’, that is an empty space.

<!DOCTYPE html> <html> <body> <script> var st = "how.are.you"; var replacedSt = st.replaceAll(".", " "); document.write("Original string : " + st); document.write("<br>Modified string : " + replacedSt); </script> </body> </html>

Using the string replace() method in JavaScript

JavaScript strings have another method called replace(). The replace() method is very much similar to the replaceAll() function. Like the previous method, this also takes two parameters, namely pattern and replacement, but the difference is that this method will replace either one or all occurrences of the pattern with the replacement provided.

How many and which occurrences will be replaced depends on how the pattern has been provided. To remove all occurrences of a pattern, we need to pass the pattern as a regular expression with the global modifier ‘g’ to this function. Hence, by using this function with regular expression as a parameter with modifier ‘g’, we can replace the dots in our string.

Let us look at the syntax and parameters of the replace() method in JavaScript.

Syntax and Parameters

The syntax of the replace() function is −

s.replace(pattern, replacement);

Here S is the string in which we want to replace some pattern. Let us look at both the parameters in detail −

  • pattern − This refers to the sub string of the string s which is to be converted into something else, the sub string that is to be replaced. This parameter can’t only be a string but can also be a regular expression that denotes the pattern to be found and replaced in string s. Only the first occurrence of the pattern in s will be replaced, but if a regular expression is provided with the global modifier ‘g’ in this parameter, all matches of that regular expression in the string s will be replaced. Naturally, this is a required parameter.

  • replacement − This denotes the string which will take the place of the pattern in the string s. This is what every occurrence of the pattern will be replaced with. Note that this parameter can also be a function that returns the string which will be used as a replacement for each occurrence of pattern in s.

This function gives another string with the required replacements as output. For example,

"aa.bb.cc.dd".replace(/\./g, " ");

The string returned after this operation will be “aa bb cc dd” instead of “aa.bb.cc.dd”, thus replacing all occurrences of dots with space. Note that the first parameter is passed as a regular expression with modifier ‘g’, because only then this function will replace all occurrences of the dots.

Example

In the below example code, we have used the regular expression /\./g which denotes all the dots anywhere in the string.

<!DOCTYPE html> <html> <body> <script> var st = "how.are.you"; var replacedSt = st.replace(/\./g, " "); document.write("Original string : " + st); document.write("<br>Modified string : " + replacedSt); </script> </body> </html>

Conclusion

In this tutorial, we saw two different methods by which we can replace all dots in a string in JavaScript. These are −

  • The string.replaceAll() method in JavaScript which replaces all occurrences of a given pattern.

  • The string.replace() method in JavaScript which replaces a particular pattern in a string.


Advertisements