Skip to content
the poor coder

replace - with space

Poor Man
Published
Reading time
2 min read
On this page

Replace - with Space#

Have you ever come across a situation where you have a string with some hyphens in it and you want to replace those hyphens with spaces? It can be a tricky task if you don't know the right way to do it.

Using String.prototype.replace()#

The easiest way to replace hyphens with spaces is by using the replace() method that is available on every string in JavaScript. The replace() method takes two arguments: the first argument is the pattern to search for (in our case, the hyphen character) and the second argument is the replacement string (in our case, a space character).

Text
const originalString = "This-is-a-string-with-hyphens";
const replacedString = originalString.replace(/-/g, " ");
console.log(replacedString); // Output: This is a string with hyphens

In the above example, we first define a string that contains hyphens. We then use the replace() method to replace all instances of the hyphen character with a space character. We use a regular expression with the global flag (/-/g) to match all hyphens in the string.

Using String.prototype.split() and Array.prototype.join()#

Another way to replace hyphens with spaces is by using the split() and join() methods. We can first split the string into an array of strings using the hyphen character as the delimiter, and then join the array back together with a space character as the separator.

Text
const originalString = "This-is-a-string-with-hyphens";
const splitString = originalString.split("-");
const replacedString = splitString.join(" ");
console.log(replacedString); // Output: This is a string with hyphens

In the above example, we first define a string that contains hyphens. We then use the split() method to split the string into an array of strings. We use the hyphen character as the delimiter. We then use the join() method to join the array back together with a space character as the separator.

Conclusion#

Replacing hyphens with spaces is a common task that you may need to do in your JavaScript projects. The replace() method and the split() and join() methods can both be used to accomplish this task. Choose the method that you find most readable and maintainable for your particular use case.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC