-2

I'm trying to match sth like html tags with regEx. It's sth like that:

Lorem <div class="sth">ipsum dolor <div class="another">sit amet, consectetur</div> adipiscing</div> elit

And my regular expression is:

/<\s*div[^>]*>(.*?)<\s*/\s*div>/g

But it should match first <div class="sth"> with last </div> and second <div class="another"> with first </div>. I want both expressions(one of them is inner and other one is outer). So I want to match the regular expressions like HTML does.

Here is the code page: https://www.regextester.com/111196

I'm using JavaScript as the programming language.

EDIT

I'm editing the question beacause of misunderstanding. I gave the expression for just understand it good. Actuall my regex is different:

/\*\*\*.*?\*\*\*.*?\*/\*\*.*?\*\*\*/g

And my text is:

Aenean ***life*** sed consectetur. ***Lorem*** ipsum dolor */**sit*** amet, consectetur */**adipiscing*** elit.

As you see ***${sth}*** is like the opening tag and */**${sth}*** is like the closing tag. 

But when I put an expression into another, it confuses. I asked how can handle this situation.

Here is the demo: https://www.regextester.com/?fam=111201

sundowatch
  • 3,012
  • 3
  • 38
  • 66

1 Answers1

0

It's best not to rely on regular expression to parse HTML, as you will always encounter exceptions that will break your regex.

Instead, add your HTML code into a temp element, that will let you use query selectors to match your tags.

let temp = document.createElement('div');
temp.innerHTML = 'Lorem <div class="sth">ipsum dolor <div class="another">sit amet, consectetur</div> adipiscing</div> elit';
divTags = temp.getElementsByTagName('div');

console.log(divTags[0].outerHTML);