user357034
user357034

Reputation: 10981

remove all <br> from a string

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("&lt;br&gt;"," ");

How would I get it to work for all <br> in the string.

Edit: this is the string...

00-6189 Start Mech Switch&lt;br&gt;00-6189 Start Mech Switch&lt;br&gt;00-6189 Start Mech Switch&lt;br&gt;

My apologies for being a little misleading with the <br> as it should have been &lt;br&gt;

Upvotes: 10

Views: 27177

Answers (5)

PowerTech
PowerTech

Reputation: 280

Try this. I am sure it will work.

productName.replace(/</?br>/g, "");

Upvotes: 0

DavideDM
DavideDM

Reputation: 1495

Using regular expression you can use this pattern

/(<|&lt;)br\s*\/*(>|&gt;)/g
productName = productName.replace(/(<|&lt;)br\s*\/*(>|&gt;)/g,' ');

That pattern matches

 <br>, <br />,<br/>,<br     />,<br  >,
 or &lt;br&gt;, &lt;br/&gt;, &lt;br /&gt;

etc...

Upvotes: 19

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196197

Looks like your string is encoded so use

productName = productName.replace(/&lt;br&gt;/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

Darin Dimitrov
Darin Dimitrov

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(/&lt;br&gt;/g, ' ');

Upvotes: 6

m4rc
m4rc

Reputation: 3006

I've not tested it but you could try something like this

productName.replace(/\<br\>/g,' ');

Upvotes: 0

Related Questions