Using javascript's replace method will only replace the first occurence of a substring in a string. To replace all occurences, you'll need to use a regular expression:
str = haystack.replace(/needle/g, 'replacement');
Notice that you don't need quotes for the needle, since it's a regular expression.