我正在做一个Handlebars.js Helper,我收到一个包含换行符(\n
)和双换行符(\n\n
)的字符串。我想替换前面带有双换行符的文本,然后用一个换行符替换相同的文本,但在<p><b></b></p>
之间。换句话说,我想要“粗体段落”中的标题和副标题,其余的则是“常规段落”。我希望这也会影响重叠匹配,但我担心.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的一部分,但我不知道是否还有其他方法可以做到。
谢谢你,对不起我的英语。
答案 0 :(得分:2)
您可以做的是将以2x \n
开头的行放入不同的捕获组,并检查您提供给{{1}的回调函数中的捕获组}。这就是你如何将段落(标题或正常)的不同“类型”捕获到不同的捕获组中(注意中间的.replace()
)
|
然后在代码中,您可以查看组1是否存在,如果是,则将(?:\r?\n){2}([^\r\n]+)|(?:^|\r?\n)([^\r\n]+)
放在其周围。否则,返回第2组:
<b></b>
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();