How to Replace Space With Underscore in JavaScript

  1. Use the replace Method to Replace Space With Underscore in JavaScript
  2. Use the split and join Methods to Replace Space With Underscore in JavaScript
How to Replace Space With Underscore in JavaScript

In JavaScript, we can replace single or multiple spaces from a string with provided string portion using the default JavaScript string methods like replace(), split(), join(), etc.

Use the replace Method to Replace Space With Underscore in JavaScript

The replace() is a pre-defined method in JavaScript, and we use it on strings to replace the defined portion of that string with another. It searches the defined string portion from the complete declared string and replaces it with the given value.

The replace() method doesn’t change the original string; it returns the updated string.

We will initialize strings containing spaces and test the replace method for replacing spaces with underscores. We will use a regular expression with the modifier set (g) to replace all instances.

<script>
let string = "Delft stack is a good website to learn programming"

let resultOne = string.replace(" ","_") //replace one
let resultAll = string.replace(/\s+/g, '_'); //replace all

  console.log("original string: "+string)
  console.log("updated string for one: "+resultOne)
  console.log("updated string for all: "+resultAll)
</script>

Output:

"original string: Delft stack is a good website to learn programming"
"updated string for one: Delft_stack is a good website to learn programming"
"updated string for all: Delft_stack_is_a_good_website_to_learn_programming"
  1. We initialized a string containing multiple spaces in the above JavaScript source code.
  2. We used replace() method on that string with 2 arguments replace(" ","_").
  3. It will find out the first " " (space) in the string and replace it with "_" (underscore).
  4. We provided regex (regular expression) to replace all the spaces in the first argument.
  5. Finally, we displayed the updated strings to see the result and differentiate the working methods.
  6. You can see the output in the console log box.

Use the split and join Methods to Replace Space With Underscore in JavaScript

In JavaScript, split() is a pre-defined method. It splits a declared string into a substring array.

The split() method doesn’t change the original string; it returns a new array of string characters.

The join() method returns a string from an array; it will not change the original array.

We can use the split() method with join() on strings to replace the defined portion of strings with another. We will initialize strings containing spaces and test the split and join methods for replacing spaces with underscores.

<script>
let string = "Delft stack is a good website to learn programming"

let result = string.split(' ').join('_');

console.log("original string: "+string)
console.log("updated string: "+result)
</script>

Output:

"original string: Delft stack is a good website to learn programming"
"updated string: Delft_stack_is_a_good_website_to_learn_programming"
  1. We initialized a string containing multiple spaces in the above JavaScript source code.
  2. We used the split() method on that string to split the string into a substring array.
  3. We used the join() method to generate a string from an array generated already with the split() method.
  4. We provided string "_" as an argument to the join method to replace all the spaces.
  5. Finally, we displayed the updated strings to see the result and differentiate the working methods.
  6. See the output in the console log box.

Related Article - JavaScript String