1var string = "my name";
2string = string.replace(/ /g,"_"); //returns my_name
1// replaces spaces with '_'
2str = str.replace(/ /g, "_");
3// or
4str = str.split(' ').join('_');
1var string = "Javascript is fun";
2var newString = string.replace(" ", "_");
3console.log(newString); // Javascript_is_fun