I’m a little disappointed JQuery doesn’t have something uber fancy for this. Something that always catches me out is when trying to replace occurrences of a string, it only replaces the first if you use the default “.replace” method.
1 |
str = str.replace("findthis","replacewiththis"); |
In order to replace all occurrences, you need to treat your JS to a little RegEx-esk treat.
1 |
str = str.replace(/findthis/g,"replacewiththis"); |
The “g” modifier is actually all it takes to do this, but obviously, it also open up a world of RegEx to play about with if you want to.
Image Credit: Lorenzoclick
Comment or tweet @douglasradburn