简体   繁体   English

移除所有 <br> 从一个字符串

[英]remove all <br> from a string

I have a string that i would like to remove all occurrences of <br> 我有一个字符串,我想删除所有出现的<br>

I tried this and it did not work. 我尝试过这个并没有用。

    productName = productName.replace("<br>"," ");

However this worked but only for the first <br> 然而,这只适用于第一个<br>

    productName = productName.replace("&lt;br&gt;"," ");

How would I get it to work for all <br> in the string. 我如何让它在字符串中为所有<br>工作。

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; 我对与所述有点误导道歉, <br> ,因为它应该是&lt;br&gt;

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. 请注意正则表达式后面的g ,表示全局,以匹配所有出现次数。

demo at http://www.jsfiddle.net/gaby/VDxHx/ 演示http://www.jsfiddle.net/gaby/VDxHx/

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... 等等...

You could use the g flag in your regular expression. 您可以在正则表达式中使用g标志。 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> . 当然,你应该知道,这不会取代<br/>也不<br />但是只有<br>

See an example of this working on ideone. 看到一个例子这个工作对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, ' ');

我没有测试过,但你可以试试这样的东西

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM