granmirupa
granmirupa

Reputation: 2790

Match open and close tag

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?

Upvotes: 1

Views: 904

Answers (2)

tdka
tdka

Reputation: 96

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

Upvotes: 1

msantos
msantos

Reputation: 791

Try it:

/(<myTag(\satt1="val1")*>)+(<\/myTag>)*/g

Upvotes: 0

Related Questions