使用RegEx提取段落标签之间的文本

问题描述:

我尝试使用javascript中的RegExp提取段落标记之间的文本.但这不起作用...

I try to extract text between parapgraph tag using RegExp in javascript. But it doen't work...

我的模式:

<p>(.*?)</p>

主题:

<p> My content. </p> <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTJ9ylGJ4SDyl49VGh9Q9an2vruuMip-VIIEG38DgGM3GvxEi_H"> <p> Second sentence. </p>

结果:

My content

我想要什么:

My content. Second sentence.

JavaScript中没有捕获所有组匹配项"(类似于PHP的 preg_match_all ),但是您可以使用.替换:

There is no "capture all group matches" (analogous to PHP's preg_match_all) in JavaScript, but you can cheat by using .replace:

var matches = [];
html.replace(/<p>(.*?)<\/p>/g, function () {
    //arguments[0] is the entire match
    matches.push(arguments[1]);
});