【问题标题】:Replace text between line breaks with <p></p> and <b></b>用 <p></p> 和 <b></b> 替换换行符之间的文本
【发布时间】:2015-01-28 09:36:09
【问题描述】:

我正在做一个 Handlebars.js 帮助程序,其中我收到一个带有换行符 (\n) 和双换行符 (\n\n) 的字符串。我想用相同的文本替换前面有一个双换行符和一个换行符的文本,但在&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt; 之间。换句话说,我想要“粗体段落”中的标题和副标题,而“常规段落”中的其余部分。我也希望这会影响重叠匹配,但恐怕.replace() 方法和我的RegEx 级别还不够。

这是我的字符串:

"Jamie Smith (known by the stage name Jamie xx) is an English music producer and remix artist, who is known both as a solo act and as a member of the London-based band The xx.
\n\n
Career:
\n\n
2005 - 2009:
\n
Smith's musical career began in 2005 when he joined The xx accompanying old schoolmates Romy Madley Croft, Oliver Sim and Baria Qureshi of the Elliott School in London, notable for alumni including Hot Chip, Burial and Four Tet. He first used the stage name Jamie xx in July 2009 in a promotional mix for the band's début album xx released on the FACT mix series of the FACT Magazine. The mixtape was compiled by Jamie Smith and featured, amongst others, four tracks credited to Jamie xx - one own-production and three remixes. The xx album went on to become platinum in the UK. Afterward, Jamie Smith went on to do more remix work for artists like Florence + The Machine, Adele, Jack Peñate and Glasser.
\n\n
2010 - Present:
\n
In late 2010, a Jamie xx remix of the song \"NY Is Killing Me\" from Gil Scott-Heron's last album I'm New Here aired on radio stations across the UK and Europe. The remix of \"I'll Take Care Of U\" followed in January 2011. Both singles drew the attention of the general public and the critics."

我想得到这个:

"<p>
Jamie Smith (known by the stage name Jamie xx) is an English music producer and remix artist, who is known both as a solo act and as a member of the London-based band The xx.
</p>
<p><b>
Career:
</b></p>
<p><b>
2005 - 2009:
</b></p>
<p>
Smith's musical career began in 2005 when he joined The xx accompanying old schoolmates Romy Madley Croft, Oliver Sim and Baria Qureshi of the Elliott School in London, notable for alumni including Hot Chip, Burial and Four Tet. He first used the stage name Jamie xx in July 2009 in a promotional mix for the band's début album xx released on the FACT mix series of the FACT Magazine. The mixtape was compiled by Jamie Smith and featured, amongst others, four tracks credited to Jamie xx - one own-production and three remixes. The xx album went on to become platinum in the UK. Afterward, Jamie Smith went on to do more remix work for artists like Florence + The Machine, Adele, Jack Peñate and Glasser.
</p>
<p><b>
2010 - Present:
</b></p>
<p>
In late 2010, a Jamie xx remix of the song \"NY Is Killing Me\" from Gil Scott-Heron's last album I'm New Here aired on radio stations across the UK and Europe. The remix of \"I'll Take Care Of U\" followed in January 2011. Both singles drew the attention of the general public and the critics.
</p>"

我正在使用 JavaScript,因为它是 Handlebars.js Helper 的一部分,但我不知道是否有其他方法。

谢谢你,对不起我的英语。

【问题讨论】:

    标签: javascript jquery regex handlebars.js


    【解决方案1】:

    您可以做的是将以 2x \n 开头的行放入不同的捕获组,并在您提供给 .replace()回调 函数中检查捕获组。这就是您如何将不同“类型”的段落(标题或普通)捕获到不同的捕获组中(注意中间的|

    (?:\r?\n){2}([^\r\n]+)|(?:^|\r?\n)([^\r\n]+)
    

    Debuggex Demo

    然后在代码中您可以查看 Group 1 是否存在,如果存在,请在其周围加上 &lt;b&gt;&lt;/b&gt;。否则,返回第 2 组:

    var subject = "Jamie Smith (known by the stage name Jamie xx) is an English music producer and remix artist, who is known both as a solo act and as a member of the London-based band The xx.\n\nCareer:\n\n2005 - 2009:\nSmith's musical career began in 2005 when he joined The xx accompanying old schoolmates Romy Madley Croft, Oliver Sim and Baria Qureshi of the Elliott School in London, notable for alumni including Hot Chip, Burial and Four Tet. He first used the stage name Jamie xx in July 2009 in a promotional mix for the band's début album xx released on the FACT mix series of the FACT Magazine. The mixtape was compiled by Jamie Smith and featured, amongst others, four tracks credited to Jamie xx - one own-production and three remixes. The xx album went on to become platinum in the UK. Afterward, Jamie Smith went on to do more remix work for artists like Florence + The Machine, Adele, Jack Peñate and Glasser.\n\n2010 - Present:\nIn late 2010, a Jamie xx remix of the song \"NY Is Killing Me\" from Gil Scott-Heron's last album I'm New Here aired on radio stations across the UK and Europe. The remix of \"I'll Take Care Of U\" followed in January 2011. Both singles drew the attention of the general public and the critics.";
    
    var regex = /(?:\r?\n){2}([^\r\n]+)|(?:^|\r?\n)([^\r\n]+)/g;
    
    var replace = subject.replace(regex, function(match, p1, p2) {
        return '\n<p>' + ((p1==undefined)? p2 : '<b>' + p1 + '</b>' ) + '</p>';
    });
    
    document.getElementById('out').value = replace.trim();
    document.getElementById('outdiv').innerHTML = replace.trim();
    <textarea rows="15" style="width:100%" id="out"></textarea>
    <div id="outdiv"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 2016-12-30
      相关资源
      最近更新 更多