js replace space with dash code example

Example 1: javascript replace spaces with dashes

title = title.replace(/\s/g , "-");

Example 2: replace all spaces with dash in javascript

title = title.replace(/\s/g , "-");
var html = "<div>" + title + "</div>";
// ...

Example 3: javascript replace all spaces with dashes

let originalText = 'This is my text';
let dashedText = originalText.replace(/ /g, '-');

Example 4: js replace space with dash

// replaces space with '-'
str = str.replace(/ /g, "-");
// or
str = str.split(' ').join('-');

Tags:

Misc Example