Flexible JavaScript – Replace in a String

Here’s yet another example of the JavaScript flexibility. You can simply call .replace() on every string and pass a regex as a parameter!

var str = 'my simple string';
str.replace(/ /g, '-'); // now 'str' will contain 'my-simple-string'

Leave a Reply

Your email address will not be published. Required fields are marked *