1

I have this string:

"<myTag att1="val1"><myTag></myTag></myTag><myTag></myTag>"

What I need is to match:

<myTag att1="val1"><myTag></myTag></myTag>

I tried with a regex but it gets:

<myTag att1="val1"><myTag></myTag>

How can I resolve?

Rob
  • 14,746
  • 28
  • 47
  • 65
granmirupa
  • 2,780
  • 16
  • 27
  • what is your regex ? I think you should make the regex lazy like this : (.+?) – Arnaud Gueras Jun 24 '16 at 12:41
  • 2
    So, you have just found out that you cannot use *JS regex to parse nested HTML tags. **JS regex does not support recursion**. Next step: learn parsing HTML with DOM. – Wiktor Stribiżew Jun 24 '16 at 12:41
  • 3
    obligatory http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – pawel Jun 24 '16 at 12:46

2 Answers2

1

Eliminate the default greedy nature and write something below:-

(.+?)

Please note, att1="val1" might not recognize in the string in few editors. Hence for that prefix a '\'. But the above regex should work

tdka
  • 86
  • 1
  • 5
0

Try it:

/(<myTag(\satt1="val1")*>)+(<\/myTag>)*/g
msantos
  • 691
  • 5
  • 6