0

i have string:

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

And now i wont find :

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

This prints my full string.

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

And sure this is good. But this is not what i want! I want this:

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

Regex must find everything between:

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

but not if between

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

This is not correct:

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

This is good:

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

What regular expression do I use to do this?

karkonosze
  • 91
  • 2
  • 8

1 Answers1

0

I fixed your regex. If you want to access what is inside the <em> and <strong> elements just add () around the [^<] section of the regex.

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

This print :

<em>ggggggg ggggggg <strong>gggggggg dddddddddd</strong></em>
Julien Grenier
  • 3,364
  • 2
  • 30
  • 43