用 <p></p> 和 <b></b> 替换换行符之间的文本
Replace text between line breaks with <p></p> and <b></b>
我正在做一个 Handlebars.js 助手,我在其中收到一个带有换行符 (\n
) 和双换行符 (\n\n
) 的字符串。我想用相同的文本替换前面有双换行符和后面有一个换行符的文本,但介于 <p><b></b></p>
之间。换句话说,我想要 "bold paragraphs" 中的标题和副标题,而 "regular paragraphs" 中的其余部分。我希望这也能影响重叠匹配,但我担心 .replace()
方法和我的正则表达式级别不够。
这是我的字符串:
"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 的一部分,但我不知道是否有其他方法可以做到这一点。
谢谢你,对不起我的英语。
您可以做的是将以 2x \n
开头的行放入不同的 capture-group 并检查 回调中的 capture-groups 你给 .replace()
的功能。这就是您如何将不同的 "types" 段落(header 或正常)捕获到不同的捕获组中(注意中间的 |
)
(?:\r?\n){2}([^\r\n]+)|(?:^|\r?\n)([^\r\n]+)
然后在代码中你可以查看组 1 是否存在,如果存在,请在其周围放置 <b></b>
。否则,return 第 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>
我正在做一个 Handlebars.js 助手,我在其中收到一个带有换行符 (\n
) 和双换行符 (\n\n
) 的字符串。我想用相同的文本替换前面有双换行符和后面有一个换行符的文本,但介于 <p><b></b></p>
之间。换句话说,我想要 "bold paragraphs" 中的标题和副标题,而 "regular paragraphs" 中的其余部分。我希望这也能影响重叠匹配,但我担心 .replace()
方法和我的正则表达式级别不够。
这是我的字符串:
"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 的一部分,但我不知道是否有其他方法可以做到这一点。
谢谢你,对不起我的英语。
您可以做的是将以 2x \n
开头的行放入不同的 capture-group 并检查 回调中的 capture-groups 你给 .replace()
的功能。这就是您如何将不同的 "types" 段落(header 或正常)捕获到不同的捕获组中(注意中间的 |
)
(?:\r?\n){2}([^\r\n]+)|(?:^|\r?\n)([^\r\n]+)
然后在代码中你可以查看组 1 是否存在,如果存在,请在其周围放置 <b></b>
。否则,return 第 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>