javascript正则表达式段落不是以句号结尾

时间:2015-08-24 16:30:01

标签: javascript regex

我有一个包含许多段落的文档。其中一些是副标题,这些副标题是可识别的,因为它们不会以句号结束,如下所示:

<p>This is a title</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a title</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a title</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>

我想让标题进入h3标签而不是句子。所以我需要找到并替换所有段落中没有结尾的段落。我需要用javascript做这个我尝试了以下但是每个都失败了。在每种情况下,文本首先被读入一个名为body的变量。

body = body.replace(/<p>(.*?)(?!\.)<\/p>/gi, "<h3>$1</h3>");

这只会让一切变得大胆

我认为这会奏效:

body = body.replace(/<p>(.*?)(?<!\.)<\/p>/gi, "<h3>$1</h3>");

但javascript无法识别背后的负面看法。

我是怎么做到的?

1 个答案:

答案 0 :(得分:3)

您可以在每个段落中执行替换段落,这对于在整个HTML上执行正则表达式会更清晰:

[].forEach.call(document.getElementsByTagName('p'), function(p){
     if (!/[.?!]\s*$/.test(p.innerHTML)) p.outerHTML="<h3>"+p.innerHTML+"</h3>";
});
<p>This is a title</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>You want to handle questions, right?</p>
<p>I'm sure you do!</p>
<p>This is a title containing 1.2 million</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a title</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>
<p>This is a sentence.</p>

这样,如果你的HTML发展就没有问题了(你真的总是只有P元素吗?)。