Reputation: 10981
I have a string that i would like to remove all occurrences of <br>
I tried this and it did not work.
productName = productName.replace("<br>"," ");
However this worked but only for the first <br>
productName = productName.replace("<br>"," ");
How would I get it to work for all <br>
in the string.
Edit: this is the string...
00-6189 Start Mech Switch<br>00-6189 Start Mech Switch<br>00-6189 Start Mech Switch<br>
My apologies for being a little misleading with the <br>
as it should have been <br>
Upvotes: 10
Views: 27177
Reputation: 280
Try this. I am sure it will work.
productName.replace(/</?br>/g, "");
Upvotes: 0
Reputation: 1495
Using regular expression you can use this pattern
/(<|<)br\s*\/*(>|>)/g
productName = productName.replace(/(<|<)br\s*\/*(>|>)/g,' ');
That pattern matches
<br>, <br />,<br/>,<br />,<br >,
or <br>, <br/>, <br />
etc...
Upvotes: 19
Reputation: 196197
Looks like your string is encoded so use
productName = productName.replace(/<br>/g," ");
note the g
after the regular expression which means globally, to match all occurrences.
demo at http://www.jsfiddle.net/gaby/VDxHx/
Upvotes: 15
Reputation: 1039238
You could use the g
flag in your regular expression. This indicates that the replace will be performed globally on all occurrences and not only on the first one.
productName = productName.replace(/\<br\>/g," ");
Of course you should be aware that this won't replace <br/>
nor <br />
but only <br>
.
See an example of this working on ideone.
UPDATE:
Now that you've provided an example with your input here's a working regex you might use to replace:
productName = productName.replace(/<br>/g, ' ');
Upvotes: 6
Reputation: 3006
I've not tested it but you could try something like this
productName.replace(/\<br\>/g,' ');
Upvotes: 0