How to replace dash with space in Javascript?

To replace dash with space in Javascript we use replace method with regex (Regular expression).

 const oldStr = 'The-text-with-dash';
 const newStr = oldStr.replace(/-/g, ' ');
 console.log(newStr) // The text with dash


You can use this method to replace anything you want like this:

 const oldStr = 'The+text+with+plus';
 const newStr = oldStr.replace(/+/g, '_');
 console.log(newStr) // The_text_with_plus


javascriptreplacereplace regex

© 2019 4codev

Created with love by Sil.