Replace All Spaces In JavaScript

To replace all spaces in JavaScript, you can use three methods –

  1. Use the replaceAll() method passing space as the first argument, and the replacement as the second argument.
  2. Use the replace method passing a regex as an argument to replace the spaces.
  3. Use the split() and join() methods to replace all spaces.
how to replace all spaces in javascript

1. The replaceAll() Method To Replace All Spaces In JavaScript

Passing space as the first argument and the replacement as the second argument to the replaceAll() method, all spaces in JavaScript can be replaced with a specific character or string.

Example:

replaceAll( ” “, “_” ); // replace all spaces with underscore

replaceAll( ” “, “” ); // remove all spaces

Here’s a complete example to replace all spaces in a string with dash using the replaceAll() method:

var str = "Hello World!";

var res = str.replaceAll( " ", "-" );

console.log(res); // Hello-World!

2. Use A Regex And replace() Method To Replace All Spaces In JavaScript

Another way to replace all spaces in JavaScript is using a regular expression. The replace() method accepts a regex as an argument and replaces all occurrences of the given regex with the replacement string.

Example:

replace(/ /g, ‘-‘); // replace all spaces with dash

replace(/ /g, ”); // remove all spaces

Here’s a complete example to replace all space characters with dashes using a regular expression and the replace() method:

var str = "Hello World!";

var res = str.replace(/ /g, '-');

console.log(res); // Hello-World!

3. Use split() And join() Methods To Replace All Spaces In JavaScript

Another way to replace all spaces in JavaScript is using the split() method which splits a string into an array of substrings, and the join() method that joins all element of an array into a string.

Example:

split(‘ ‘).join(‘-‘); // replace all spaces with dash

split(‘ ‘).join(”); // remove all spaces

Here’s a complete example to replace all spaces with dashes using the split() and join() methods:

var str = "Hello World!";

var res = str.split(' ').join('-');

console.log(res); // Hello-World!

Let’s look at these methods in detail below.

replaceAll() Method In JavaScript

The replaceAll() method takes two parameters –

  • first string to be replaced
  • second string to replace with

When called, the replaceAll() method searches the string for all instances of the first string and replaces them with the second string.

This method is case sensitive.

Syntax:

str.replaceAll(regexp|substr, newSubstr|function)

Example 1: This example uses the replaceAll() method to replace all spaces with hyphen.

JavaScript Code:

var str = "Hello world! Welcome to my website.";

var result = str.replaceAll(" ", "-");

console.log(result);

Output: Hello-world!-Welcome-to-my-website.

Example 2: This example replaces all occurrences of the letter ‘o’ with the letter ‘i’.

JavaScript Code:

var str = "Hello World!";

var result = str.replaceAll("o", "i");

console.log(result);

Output: Helli Wirld!

Example 3: This example replaces all occurrences of the word ‘Welcome’ with the word ‘Greetings’.

JavaScript Code:

var str = "Hello world! Welcome to my website.";

var result = str.replaceAll("Welcome", "Greetings");

console.log(result);

Output: Hello world! Greetings to my website.

replace() Method In JavaScript

The replace() method takes two parameters –

  • first string or regular expression to be replaced
  • second string to replace with

When called, the replace() method searches the string for the first instance of the first parameter and replaces them with the second parameter.

This method is case sensitive.

Syntax:

str.replace(regexp|substr, newSubstr|function)

Example 1: This example replaces all spaces with hyphen.

JavaScript Code:

var str = "Hello world! Welcome to my website.";

var result = str.replace(/ /g, "-");

console.log(result);

Output: Hello-world!-Welcome-to-my-website.

Example 2: This example replaces all occurrences of the letter ‘o’ with the letter ‘i’.

JavaScript Code:

var str = "Hello World!";

var result = str.replace(/o/g, "i");

console.log(result);

Output: Helli Wirld!

Example 3: This example replaces all occurrences of the word ‘Welcome’ with the word ‘Greetings’.

JavaScript Code:

var str = "Hello world! Welcome to my website.";

var result = str.replace(/Welcome/g, "Greetings");

console.log(result);

Output: Hello world! Greetings to my website.

split() And Join() Methods In JavaScript

The split() method is used to split a string into an array of substrings, and returns the new array. The join() method joins all elements of an array into a string. When combined, they can be used to replace all occurrences of a given character or substring with another character or string.

Example 1: This example uses the split() and join() methods to replace all occurrences of the letter ‘o’ with the letter ‘i’.

JavaScript Code:

var str = "Hello world!";

var res = str.split("o").join("i");

console.log(res);

Output: Helli wirld!

Example 2: This example replaces all occurrences of the word ‘Welcome’ with the word ‘Greetings’.

JavaScript Code:

var str = "Hello world! Welcome to my website.";

var res = str.split("Welcome").join("Greetings");

console.log(res);

Output: Hello world! Greetings to my website.

Conclusion

In this article, you learned about two methods to replace all spaces in a string with JavaScript. The first method uses the replaceAll() method and the second method uses a regular expression and the replace() method.

You can also use the split() and join() methods to remove all spaces from a string.

I hope this article was helpful. Thank you for reading!

Leave a Reply