JavaScript

Replace All Spaces in a String With ‘+’ in JavaScript

Sometimes, programmers need to replace spaces in a string with a special specified character. It is often done to format the string for a specific purpose, such as URL encoding by replacing spaces with “%20”, creating a valid file name by replacing spaces with ‘_’ or ‘’, for data processing, and so on.

This post will illustrate the methods for replacing all the spaces in a string with a”+” sign in JavaScript.

How to Replace All Spaces in a String With “+” in JavaScript?

For replacing spaces from a string with “+”, use the given methods:

Method 1: Replace All Spaces in a String With “+” Using “replace()” Method

Use the “replace()” method for replacing all the spaces in a string. It searches a string for a particular value or a regular expression/pattern and outputs a new string where the certain values are replaced. It gives a new string and does not modify the original string.

Syntax

Use the given-mentioned syntax for the replace() method:

replace(searchValue, replacementValue)

Here, the “searchValue” is the value or regular expression/regex pattern to search for, and the “replacementValue” is the value to replace the search value with.

Example

Firstly, create a string:

const string = "Welcome to Linuxhint";

Call the “replace()” method by passing the regex pattern for spaces and a “+” sign as a replacement argument:

const newString = string.replace(/ /g, '+');

The regular expression is used for replacing all the spaces because the replace method only replaces the first instance of the string.

Print the returned string from the replace() method on the console:

console.log(newString);

It can be seen that the “+” sign has successfully replaced the spaces in the string:

Method 2: Replace All Spaces in a String With “+” Using “replaceAll()” Method

You can also use the “replaceAll()” method as it replaces all the occurrences of the specified value with the particular character. It also outputs a newly created string with replaced values.

Syntax

The following syntax is used for the replaceAll() method:

replace(searchValue, replacementValue)

In this method, there is no need to use the regex pattern for searching the string.

Example

Call the replaceAll() method and pass the space and the “+” as a parameter that will replace all spaces with a “+” sign:

const newString = string.replaceAll(' ', '+');

Output

Method 3: Replace All Spaces in a String With “+” Using “split()” Method With “join()” Method

Use the “split()” method with the “join()” method to replace the spaces in a string with “+”. The split() method converts a string into an array/list of substrings based on a specified separator. The join() method can then combine the substrings in a single string with a particular separator.

Syntax

The given provided syntax is utilized for the split() with the join() method:

split(separator).join('character')

Example

Invoke the split() method with the given string to split it into substrings by passing the space separator “ ” and then call the join() method to join the substrings with the “+” sign:

const newString = string.split(' ').join('+');

As you can see that the string has successfully replaced the spaces with the “+”:

We have gathered all the essential instructions related to replacing all spaces in a string with “+” in JavaScript.

Conclusion

To replace all spaces from a string with “+”, use the “replace()” method, “replaceAll()” method, “split()” method with “join()” method. replace() method only replaces the first instance, for all instances utilize the regular expression or pattern. While the replaceAll() method and the split() method with join() methods take the searched value as a space (“ ”) instead of a regex pattern. In this post, we illustrated the methods for replacing all the spaces in a string with a”+” sign in JavaScript.

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.