I have scoured the Internet for an answer to this. I have an array of strings that looks something like this:
[0] Cinnamon+Chocolate+Twists
[1] 3.25+Ounce+Croissant
I need all the ‘+’ characters removed from these strings, and a space inserted instead. The following code does not work:
nameProduct[1] = nameProduct[1].replace("+"," ");
I have tried several variations of the .replace method, but no luck. Any ideas?
You have to use regular expression with the
global flag to replace every occurrence of the character (yes, JavaScript behaves weird in this aspect (or let’s say unexpected, depending from which other language you come)):Use a loop to do this for every element.
MDC –
replacedocumentationUpdate:
Don’t use
document.writeto create HTML. Your code needs a lot of refactoring but this beyond this question. The data is processed correctly, but the generated HTML is not:You see, you have problems with the quotes. The other parts of the text are recognized as attributes.
A slightly improved version would be (just this part):
DEMO