jQuery Replace craziness

 

I spent way too much time figuring this out.

var originalString = “Batman And Robin”;

var replacedSpaces = originalString.replace(‘ ‘, ‘_’);

This doesn’t work, it only replaces the first instace of the match, so replacedSpaces == “Batman_And Robin”.

var replacedSpaces = originalString.replace(/ /g, ‘_’);

This does work, and now replacedSpaces == “Batman_And_Robin”.

 

Leave a Reply