本文介绍了使用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]);
});
这篇关于使用RegEx提取段落标签之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!