JavaScript

How to Replace Space with Underscore in JavaScript

JavaScript is a scripting language that is utilized for developing attractive and interactive web pages for websites. However, sometimes, developers need to replace some characters or extra spaces from the text with specified letters. For the corresponding purpose in JavaScript, we have some predefined methods which are utilized.

This tutorial will illustrate the procedure of replacing spaces with underscores.

How to Replace Space with Underscore in JavaScript?

For replacing spaces in a string with an underscore, there are some predefined methods of JavaScript that are listed below.

    • replace() method
    • replaceAll() method
    • split() method

Let’s look at the working of each method.

Method 1: Replace Space with Underscore Using replace() Method

The “replace()” method will simply replace the value in a string with the defined string It accepts two parameters, one is the value that will be replaced, and the other is the value used as a replacer. In our case, we will remove the space in a string with an underscore, so, the space is the replaced value that will be searched in a string, and the underscore will act as the replacer.

Note: This method only works for replacing a single value.

Syntax

Follow the given syntax to use the replace() method:

replace("replaceValue","replacer");

 
Example

Here, we will create a variable “str” assigned with a string “Linux Hint” that contains space in between both words:

var str = "Linux Hint";

 
Now, we will call the replace() method by passing the value that needs to be replaced and a replacer as the space “ ” and the underscore “_”, respectively:

console.log(str.replace(" " , "_"));

 
As you can see, we have successfully replaced space with the underscore in the specified string:


If you want to replace all the spaces in a string, you can follow the next given section.

Method 2: Replace Space with Underscore Using replaceAll() Method

The “replaceAll()” method is also a predefined method of JavaScript. This method also takes two parameters, one is the value that will be replaced, and the other is the value that is used as a replacer. It is specifically used in cases when it is required to replace all of the specified values at once.

This method replaced all the string’s entire spaces.

Syntax

Follow the given syntax to use the replaceAll() method:

replaceAll("replaceValue","replacer");

 
Example

Here, we will create a variable “str” assigned with a string “Welcome to Linux Hint” that contains multiple spaces going to be replaced with underscores:

var str = "Welcome to Linux Hint";

 
Now, we will call the replaceAll() method by passing space “ ” and the underscore “_” as arguments:

console.log(str.replace(" " , "_"));

 
The output shows we have successfully replaced all spaces with the underscores in the given string:


Let’s move to the other method for replacing space with an underscore.

Method 3: Replace Space with Underscore Using split() Method

The string is divided into an array of substrings with the help of the “split()” method. For replacing any character or space from a string with a specified character, you can use the “join()” method. The split() method takes a parameter that needs to be replaced, and the join() method accepts the replacer as its argument.

This method finds the spaces in the string and replaces all the spaces with the specified character.

Syntax

The given syntax is used for replacing space with an underscore:

split(“ ”).join(“_”)

 
Example 1: Replace Single Space with Underscore

For replacing space with an underscore, we will utilize the already created string. To do so, we will call the split() method with join() method by passing space and underscore as parameters, respectively:

console.log(str.split(“ ”).join(“_”));

 
While executing the above code, you can see the string replaced with an underscore:


Example 2: Replace Multiple Spaces with Underscores

Here, we will remove all the entire spaces from a string with the help of split() method. First, we will create a string “Welcome to Linux Hint” with multiple spaces:

var str = "Welcome to Linux Hint";

 
Now, we will call the split() method with the join() method and print the resultant string using “console.log()” method:

console.log(str.split(“ ”).join(“_”));

 

We have provided simplest methods to replace space with an underscore.

Conclusion

For replacing space with an underscore, you can use the predefined methods of JavaScript, including replace() method, replaceAll() method, and the split() method. The split() method is utilized with the join() method. The replace() method is used to replace a single space in a string; if you want to replace all the spaces in a string, utilize the replaceAll() method or the split() method. In this tutorial, we have illustrated the procedure to replace the spaces with underscore with detailed examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.