2013-05-27 56 views
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> 

我用什麼正則表達式來做到這一點?

+8

[強制性](http://stackoverflow.com/a/1732454/1223693) – Doorknob

回答

0

我修復了你的正則表達式。如果您想訪問<em><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> 
+0

THX這是爲我好 – karkonosze