Skip to content
Related Articles

Related Articles

How to replace all dots in a string using JavaScript?

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Jul, 2022

There are some methods to replace the dots(.) in the string.

  • replace(): The string.replace() function is used to replace a part of the given string with some another string or a regular expression. The original string will remain unchanged. 
  • Syntax:
str.replace(A, B)
  • Example-1: we are replacing the dots(.) with space( ) in the text “A.Computer.science.Portal”. 

javascript




<!DOCTYPE html>
<html>
 
<head>
</head>
 
<body>
    <center>
        <h1 style="color:green">
          GeeksforGeeks
      </h1>
        <script>
           
            // Assigning a string
            var str = 'A.Computer.science.Portal';
 
            // Calling replace() function
            var res = str.replace(/\./g, ' ');
 
            // Printing original string
            document.write("String 1: " + str);
           
            // Printing replaced string
            document.write("<br>String 2: " + res);
           
        </script>
    </center>
</body>
 
</html>

  • Output:
  • Split() and Join(): We can split up strings of text with the Javascript split method and join strings using the replace characters with the join method. Syntax:
string.split('.').join(' ');
  • Example-2: we are replacing the dots(.) with space( ) using split and join. 

javascript




<!DOCTYPE html>
<html>
   <head>
   </head>
   <body>
       <center>
      <h1 style="color:green">GeeksforGeeks</h1>
      <script>
          
          // Assigning a string
          let str = 'A.Computer.Science.portal';
           
          // Calling split(), join() function
          let newStr = str.split('.').join(' ');
           
          // Printing original string
         document.write("String 1: "+str);
          
         // Printing replaced string
         document.write("<br>String 2: "+newStr);
      </script>
      </center>
   </body>
</html>

  • Output:
  • reduce() and spread operator: We can use spread operator to make array from character of string and form string with the help of reduce without dots in string.
  • Syntax:
[...str].reduce( (accum, char) => (char==='.') ? accum : accum + char , '')
  • Example#3: In this example we will be replace ( ‘.’ )  by using spread operator and reduce function.

HTML




<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <center>
    <h1 style="color:green">GeeksforGeeks</h1>
    <script>
         
        // Assigning a string
        let str = 'A.Computer.Science.portal';
             
        // using reduce(), and sprea operator
        let newStr = [...str].reduce((s, c) => (c === '.') ? s : s+c, '');
             
        // Printing original string
        document.write("String 1: "+str);
         
        // Printing replaced string
        document.write("<br>String 2: "+newStr);
    </script>
    </center>
</body>
</html>

  • Output:

reduce() method


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!