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>
Remove All Line Breaks from a String We can remove all line breaks by using a regex to match all the line breaks by writing: str = str. replace(/(\r\n|\n|\r)/gm, ""); \r\n is the CRLF line break used by Windows.
I found this in JavaScript line breaks: someText = someText. replace(/(\r\n|\n|\r)/gm, ""); That should remove all kinds of line breaks.
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...
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/
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, ' ');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With