How To Remove All Hyphens From A String In JavaScript

How to Remove all Hyphens from a String in JavaScript

In JavaScript, to work with strings, we have a lot of built-in methods to help us do this. As for removing all hyphens from a string, we can do it easily with some available methods like using replace() or replaceAll() method. Let’s follow this article to know how to remove all hyphens from a string in JavaScript.

Remove all hyphens from a string in JavaScript?

Use replace() method

replace() is a method that helps us to change the character string we pass into the desired string or character.

Syntax:

string.replace(searchValue, newValue)

Parameters:

  • searchValue: The string that you want to change to another string.
  • newValue: is the string you want to change.

Example:

var myStr = "LearnShareIT-Hello-world";

// Use replace() method remove all hyphens
var newStr = myStr.replace("-", " ");
console.log(newStr);

Output:

LearnShareIT Hello-world

We see in this example that we have a new string with the change in the first dash, but different from what we expected in this post because it can only remove one dash in the string that we require. That’s all.

To use the replace() method to remove all hyphens from a string, we need to use more regular expressions to support it. I would remove all hyphens from a string in JavaScript as follows:

var myStr = "LearnShareIT-Hello-world";

// Use replace() method remove all hyphens
var newStr = myStr.replace(/-/g, " ");
console.log(newStr);

Output:

LearnShareIT Hello world

With regular expression, (g) means only global. Here is the whole string, so when we use this regular expression, the program will understand to remove all dashes of the post and replace the positions. The deletion to a space.

Use the replaceAll() method

replaceAll() method is similar in syntax to the value passed, but their return results differ. One side returns a string that has been replaced, and for replaceAll(), it will return a new string that has been replaced. Change all the same as the string we entered in the parameter section.

Syntax:

string.replaceAll(searchValue, newValue)

Parameters:

  • searchValue: The string that you want to change to another string.
  • newValue: is the string you want to change.

Example:

var myStr = "LearnShareIT-Hello-world";

// Use replaceAll() method remove all hyphens
var newStr = myStr.replaceAll("-", " ");
console.log(newStr);

Output:

LearnShareIT Hello world

This way, we can combine it with a regular expression to remove all hyphens from a string. If desired, pass in (-), and a replacement string, and the method will return the result as you expect.

Summary

Above are the two ways I usually use to remove all hyphens from a string in JavaScript. I prefer using the replaceAll() method because it is faster and easier to implement. You can try both ways. I hope this article helps you. Good luck!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *