This article discusses replacing all spaces in a javascript string with underscore using different methods and illustrations.

Table of Contents:-

Javascript string replace all spaces with underscores using replace() method

The javascript replace() method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string, or regExp.

Syntax:-

replace(regexp, newSubstring)
replace(stringToBeReplaced, newSubstring)

Example:-

Replace all occurrences of ” ” space with “_” underscore from the string “Javascript is the most popular language”

let dummyString = "Javascript is the most popular language";
newDummyString = dummyString.replace(/ /g, "_");
console.log("Old String is: "+ dummyString); 
console.log("New String is: "+ newDummyString); 

OR

let dummyString = "Javascript is the most popular language";
newDummyString = dummyString.replace(/\s+/g,"_");
console.log("Old String is: "+ dummyString); 
console.log("New String is: "+ newDummyString); */

Output:-

Old String is: Javascript is the most popular language
New String is: Javascript_is_the_most_popular_language

Javascript string replace all spaces with underscores using replaceAll() method

The replaceAll() method in javascript returns a new string with all matches of a pattern replaced by a replacement(character/string).

Syntax:-

replaceAll(regexp, newSubstring)
replaceAll(stringToBeReplaced, newSubstring)

Example:-

Replace all occurrences of ” ” spaces with “_” underscore from the string “Javascript is the most popular language”

let dummyString = "Javascript is the most popular language";
newDummyString = dummyString.replaceAll(" ", "_");
console.log("Old String is: "+ dummyString); 
console.log("New String is: "+ newDummyString);

Output:-

Old String is: Javascript is the most popular language
New String is: Javascript_is_the_most_popular_language

Javascript string replace all spaces with underscores using split and join

This section will replace all ” ” spaces in the javascript string by splitting and joining an array. The solution is to split the original string by ” ” space and then replace it with “_” underscore. Then, finally, joining back into one string.

Syntax:-

newString = originalString.split(" ").join("_")

Example:-

Replace all occurrences of ” ” spaces with “_” underscore from the string “Javascript is the most popular language”

let dummyString = "Javascript is the most popular language";
newDummyString = dummyString.split(' ').join('_')
console.log("Old String is: "+ dummyString); 
console.log("New String is: "+ newDummyString);

Output:-

Old String is: Javascript is the most popular language
New String is: Javascript_is_the_most_popular_language

Javascript string replace all spaces with underscores using substring() and indexOf() methods

This section will replace all occurrences of ” ” spaces in the javascript string with the “_” underscore. Observe the below code:

Example:-

Replace all occurrences of ” ” spaces with “_” underscore from the string “Javascript is the most popular language”

let dummyString = "Javascript is the most popular language";
let i=-1;
let findToken = ' '
let newToken ='_'
 while (( i=dummyString.indexOf(findToken,i>=0? i=newToken.length : 0 )) !== -1
 )
 { dummyString= dummyString.substring(0,i) + newToken + dummyString.substring(i+findToken.length)} 
console.log(dummyString)

Output:-

Javascript_is_the_most_popular_language

Explanation:-

  • Finding the indexOf of  ” ” space.
  • Identify the ” ” space to be replaced based on the index and then replace it with “_” underscore.
  • Finally, concatenating the substrings to one string.

Read More:

We hope this article helped you replace all occurrences ” ” spaces in a javascript string with an “_” underscore. Good Luck !!!