/ Published in: JavaScript
Expand |
Embed | Plain Text
var str = "This string has too many spaces."; var result = str.replace(/\s/gi, "_"); document.write(result);
Comments
Subscribe to comments
You need to login to post a comment.

You're missing out a couple of items: because you're using Javascript, keep in mind of ( you can also look at - I think). Your script also treats multiple spaces/tabs/white spaces as it's own entity, which could lead to undesired results. An alternative is:
var result = str.replace(/(\s| | )+/gi, "_");
Ugh, don't know why it didn't show up properly - I've put the code here: http://snipplr.com/view/14252/replace-space-with-/