简体   繁体   中英

Regex to match all characters wrapped in a div tag

<div class="myClass">
<form method = "post" name="loginForm" >
<input type="text" name="userName"/>
</form>
<form method = "post" name="signupForm" >
<input type="text" name="userName"/>
</form>
</div>

if this was to be an example html.

<div>(.*?)</div>

So, something like this, anything (even nested tags) between opening and closing div tag

Perhaps help you :

(<[div]+\s[a-zA-Z]+\=\"[a-zA-Z]+\">)(.*)(<\/[div]+>)

在此处输入图片说明

URL : https://www.regex101.com/

I had to do this a few years ago, but with additional flexibility (capturing content of no matter what element):

<(?<element>\w+).*?>(?<content>.*)?</[\s]*\1>

Which works quite okay, but I have to agree with the comments above; if you can, use a fully fledged DOM parser! It'll handle edge cases etc...

Not if you really just want to get content of <div /> 's that would be:

<(div).*?>(?<content>.*)?</[\s]*\1>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM