Thursday, July 4, 2024
HomeLanguagesJavascriptHow to replace all dots in a string using JavaScript?

How to replace all dots in a string using JavaScript?

We will replace all dots in a string using javascript. There are 4 methods to replace the dots(.) in the string all of them are described with examples.

Methods to Replace All Dots in a String:

Method 1: 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: 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

Method 2: JavaScript Split() and Join() Method

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: 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

Method 3: JavaSccript reduce() Method and JavaScript 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 = "A.Computer.Science.portal";
 
// 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: A.Computer.Science.portal
String 2: AComputerScienceportal

Method 4: JavaScript replaceAll() Method

The 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 : neveropen

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments