0

我有字符串:

<em>hhhhhhhhhhhhh gggggggggggg hhhhhhhhhhh</em> <strong>hhhhhhhhhh hhhhhhhhh hhhhhhhhh</strong> gggggggggg ggggggg hhhhhhhhhhh ggggggggggggggggggggggggg <em>ggggggg ggggggg <strong>gggggggg dddddddddd</strong></em>

现在我找不到:

var regex = /<em>(.*?)<\/strong><\/em>/g;
var spr = string.match(regexxx);
alert(spr.toString());

这将打印我的完整字符串。

<em>hhhhhhhhhhhhh gggggggggggg hhhhhhhhhhh</em> <strong>hhhhhhhhhh hhhhhhhhh hhhhhhhhh</strong> gggggggggg ggggggg hhhhhhhhhhh ggggggggggggggggggggggggg <em>ggggggg ggggggg <strong>gggggggg dddddddddd</strong></em>

当然这很好。但这不是我想要的!我要这个:

<em>ggggggg ggggggg <strong>gggggggg dddddddddd</strong></em>

正则表达式必须找到以下之间的所有内容:

<em>...</strong></em>

但如果介于

<em>..**</em>**.</strong></em> .

这是不正确的:

<em>testestest</em>test test</strong></em> 

这很好:

<em>test<strong>test test</strong></em>

我用什么正则表达式来做到这一点?

4

1 回答 1

0

我修复了你的正则表达式。如果您想访问<em>and<strong>元素中的内容,只需在正则表达式的部分() 周围添加即可。[^<]

var regex = /<em>[^<]*?<strong>[^<]*?<\/strong><\/em>/g;
var result = regex.exec(s)
console.log(result[0]);

这个打印:

<em>ggggggg ggggggg <strong>gggggggg dddddddddd</strong></em>
于 2013-05-27T13:37:07.990 回答