Open In App

How to replace all dots in a string using JavaScript ?

Last Updated : 13 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript.

Using JavaScript replace() Method

The string.replace() function is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged. 

Syntax:

str.replace(A, B)

Example: Here we are replacing the dots(.) with space( ) in the text “A.Computer.science.Portal”. 

javascript
// Assigning a string
let str = "A.Computer.science.Portal";

// Calling replace() function
let res = str.replace(/\./g, " ");

// Printing original string
console.log("String 1: " + str);

// Printing replaced string
console.log("String 2: " + res);

Output
String 1: A.Computer.science.Portal
String 2: A Computer science Portal

Using JavaScript Split() and Join() Method

We can split up strings of text with the JavaScript split() method and join() method to join strings using the replace characters with the join method. 

Syntax:

string.split('.').join(' ');

Example: Here we are replacing the dots(.) with space( ) using split and join. 

javascript
// Assigning a string
let str = "A.Computer.Science.portal";

// Calling split(), join() function
let newStr = str.split(".").join(" ");

// Printing original string
console.log("String 1: " + str);

// Printing replaced string
console.log("String 2: " + newStr);

Output
String 1: A.Computer.Science.portal
String 2: A Computer Science portal

Using JavaSccript reduce() Method and spread operator

We can use the spread operator to make an array from the character of a string and form a string with the help of reduce() method without dots in the string.

Syntax:

[...str].reduce( (accum, char) => (char==='.') ? accum : accum + char , '')

Example: In this example, we will replace ( ‘.’ )  by using the spread operator and reduce function.

Javascript
// Assigning a string
let str = "Geeks.for.Geeks";

// using reduce(), and sprea operator
let newStr = [...str].reduce(
    (s, c) => (c === "." ? s : s + c),""
);

// Printing original string
console.log("String 1: " + str);

// Printing replaced string
console.log("String 2: " + newStr);

Output
String 1: Geeks.for.Geeks
String 2: GeeksforGeeks

Using JavaScript replaceAll() Method

JavaScript replaceAll() method returns a new string after replacing all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation.

Syntax:

const newString = originalString.replaceAll(regexp | substr , newSubstr | function)

Example: This example uses the replaceAll() method to replace  ( ‘.’ ).

Javascript
let str = "Geeks.for.Geeks";
let replacedStr = str.replaceAll(".", "");

console.log("Original string : " + str);
console.log("Modified string : " + replacedStr);

Output
Original string : Geeks.for.Geeks
Modified string : GeeksforGeeks

Using JavaScript for loop

We iterates over each character in the string using a for loop. If the current character is a dot (‘.’), it appends the newChar character to the result; otherwise, it appends the current character from the input string. Finally, it returns the result string, which contains the replaced characters.

Example: This example uses the replaceAll() method to replace  ( ‘.’ ).

Javascript
const originalString = "Hi,.Welcome.to.GeeksforGeeks";
 let replacedStr = '';
    for (let i = 0; i < originalString.length; i++) {
        if (originalString[i] === '.') {
            replacedStr += ' ';
        } else {
            replacedStr += originalString[i];
        }
    }

console.log("Original string : " + originalString);
console.log("Modified string : " + replacedStr);

Output
Original string : Hi,.Welcome.to.GeeksforGeeks
Modified string : Hi, Welcome to GeeksforGeeks




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads