replace dash with space in javascript

To replace dash (-) with a space in a string in JavaScript, you can use the .replace() method of the string object with a regular expression.

index.tsx
let string_with_dash = "this-is-a-string-with-dash";
let string_with_space = string_with_dash.replace(/-/g, ' ');

console.log(string_with_space); // "this is a string with dash"
179 chars
5 lines

In this code, the regular expression /-/g matches all instances of the dash character globally throughout the string, and replaces them with a space ' '.

gistlibby LogSnag