How to replace all dots in a string using JavaScript ?
Last Updated :
24 May, 2024
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);
OutputString 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);
OutputString 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);
OutputString 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);
OutputOriginal 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);
Using JavaScript map() Method on Arrays
We can convert the string into an array of characters, use the map() method to iterate over each character, and replace dots with spaces. Finally, we join the array back into a string.
Example: In this example, we replace dots (‘.’) with spaces (‘ ‘) using the map() method.
JavaScript
// Assigning a string
let str = "Learning.is.fun.with.JavaScript";
// Using map() to replace dots with spaces
let newStr = Array.from(str).map(char => char === '.' ? ' ' : char).join('');
// Printing original string
console.log("String 1: " + str);
// Printing replaced string
console.log("String 2: " + newStr);
OutputString 1: Learning.is.fun.with.JavaScript
String 2: Learning is fun with JavaScript
Similar Reads
How to Get Character of Specific Position using JavaScript ?
Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1
4 min read
Remove a Character From String in JavaScript
In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like: Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods
3 min read
Reverse a String in JavaScript
We have given an input string and the task is to reverse the input string in JavaScript. Using split(), reverse() and join() MethodsThe split() method divides the string into an array of characters, reverse() reverses the array, and join() combines the reversed characters into a new string, effectiv
1 min read
JavaScript - Convert String to Title Case
Converting a string to title case means capitalizing the first letter of each word while keeping the remaining letters in lowercase. Here are different ways to convert string to title case in JavaScript. 1. Using for LoopJavaScript for loop is used to iterate over the arguments of the function, and
4 min read
JavaScript - Sort an Array of Strings
Here are the various methods to sort an array of strings in JavaScript 1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values. [GFGTABS] JavaScript let a
3 min read
How to Convert String to Camel Case in JavaScript?
We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meanin
4 min read
Extract a Number from a String using JavaScript
We will extract the numbers if they exist in a given string. We will have a string and we need to print the numbers that are present in the given string in the console. Below are the methods to extract a number from string using JavaScript: Table of Content Using JavaScript match method with regExUs
4 min read
JavaScript - Delete First Character of a String
To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common ones Using slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end. [GFGTABS] JavaScript let s1 = "Geeksfor
1 min read
JavaScript - How to Get Character Array from String?
Here are the various methods to get character array from a string in JavaScript. 1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. [GFGTABS] JavaScript let
2 min read
JavaScript - How To Get The Last Caracter of a String?
Here are the various approaches to get the last character of a String using JavaScript. 1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1. [GFGTABS] JavaScript const s =
3 min read