Global Flag

'This is my sentence'.replace(/ /g, '_');  
// => "This_is_my_sentence"

This will simply replace all spaces (/ /g) with an underscore. Notice the g which stands for globally, so all "matched" strings will get replaced. To only replace the first occuring match, leave the /g, e.g.

'This is my sentence'.replace(/ /, '_');  
// => "This_is my sentence"