【问题标题】:replace two br with single one jquery用一个 jquery 替换两个 br
【发布时间】:2013-11-27 13:26:01
【问题描述】:

试图用一个替换两个<br />
不能让它工作。尝试了几个小时。

我的代码:thishtml = thishtml.replace(/(?:<br \/\>\s*){2,}/g, '<br>')

不工作。
帮助会很大。

先谢谢了。

【问题讨论】:

标签: javascript jquery regex line-breaks


【解决方案1】:
$('br').map(function(){

  ($next = $(this).next()).is('br') && $next.remove();

});

如果您的 html 不是来自 DOM:

$(thishtml).find('br').map( ... )

【讨论】:

  • 如果他在 DOM 上操作,那就太好了。虽然我们不知道他的 html 来自哪里
  • @CrayonViolent 然后$(someHtml).find('br').map( ... )^^
【解决方案2】:

试试这个:

.replace(/(\<br[\s]*\/\>){2,}/g, '<br/>')

【讨论】:

    【解决方案3】:

    试试这个:

    .replace(/(?:&lt;\s*br\s*\/?\&gt;\s*){2,}/ig, '&lt;br/&gt;');

    例子:

    var a='sdf sd\
    sdf\
    sdf\
    <br    />\
        <br/>\
      dfbd'
    

    结果:

    sdf sdsdfsdf&lt;br/&gt;dfbd

    http://jsbin.com/OfEbaCA/6/edit

    【讨论】:

      【解决方案4】:

      试试

      thishtml = thishtml.replace(/(?:&lt;br[^&gt;]*&gt;\s*){2,}/g, '&lt;br&gt;')

      【讨论】:

        猜你喜欢
        • 2013-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-29
        • 2015-08-15
        • 1970-01-01
        相关资源
        最近更新 更多