replace all space with backslash in string javascript code example Example: js replace all spaces var replaced = str.replace(/ /g, '_');