Javascript: Remove dots from a string

This article will discuss removing dots from a javascript string using simple methods and example illustrations.

Table of Contents:-

Javascript remove all dots from a string using replace()

Javascript’s replace() method will replace a particular pattern in the calling string with a replacement. The pattern being the first argument can be a regular expression or a function. The replacement is the second argument and is optional.

Example:-

Remove all dots from the string “This.is.javascript.”

Code:-

let dummyString = "This.Is.Javascript."
dummyString = dummyString.replace(/\./g,'');
console.log(dummyString)

Explanation:-

The regular expression /\./g is the first argument of replace() method where

  • / and / mark the beginning and end of the pattern
  • \. specifies to match all dots in the calling string
  • g specifies to replace all occurrences.

The second argument is the replacement (”), which means nothing.

Output:-

ThisIsJavascript

Javascript remove backslash escape from a string using replaceAll()

The javascript’s replaceAll() method will replace all the occurrences of a particular character/string within the calling string with a replacement. The first argument is the pattern. The replacement is the second argument.

Example:-

Remove all the occurrences of dots from the string “This.Is.Javascript.”

Code:-

let dummyString = "This.Is.Javascript."
dummyString = dummyString.replaceAll('.','')
console.log(dummyString)

Explanation:-

The replaceAll() method is used to find the dots using the regular expression (\.) passed in as the first argument and replace all its occurrences with (”) passed as the second argument.

Output:-

ThisIsJavascript

Javascript remove all dots from a string using spilt() and join()

Javascript’s split() method returns an array of substrings formed by splitting a given string.

Javascript’s join() method joins the elements of the array back into a string.

Example:-

Remove all the occurrences of dots from the string “This.Is.Javascript.”

Code:-

let dummyString = "This.is.javascript."
dummyString = dummyString.split('.').join('')
console.log(dummyString)

Explanation:-

In the above code, the split() method splits the original string into an array of substrings. The division is done based on dots (.). Then, using the join() method, these array elements are joined back to form a string without dots.

Output:-

ThisIsJavascript

Javascript remove dots from a string using a loop

Example:-

Remove all the occurrences of dots from the string “This.Is.Javascript.”

Function Code:-

function removeDots(_string){
    let dummyString = String(_string);
    let finalString =''
    for( let i = 0; i < dummyString.length; i++){
        if(!(dummyString.charAt(i) === ".") ){
            finalString = finalString + dummyString.charAt(i);
        }
    }
    return finalString;
  }

Usage:-

let inputString = "This.Is.Javascript."
console.log(removeDots(inputString))

Explanation:-

Output:-

The above code uses the loop method to iterate the string and traverse each character to see if a dot is present. Within the loop, a new string with all the characters of the old string except dots is created. Finally, return the string without dots.

ThisIsJavascript

Read More:

I hope this article helped you to delete dots from a string in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top