regex - Using the JavaScript regular expression flavour, how to match a specific tag only when inside another specific tag? -


i want match <br> tags inside <main> tag , not of them:

enter image description here

is possible js regex? i'm trying find , replace (with regex) in files in project.

here's raw text:

<br> <main>     <input>     <br>     <hr>     <br>     <etc> </main> 

using dom better parsing html text. if reason cannot use dom here regex solution match <br> tags between <main> , </main>.

/<\s*br\s*\/?>(?=.*?(?:(?!<main>)[\s\s])*?<\/main>)/gi 

regex breakup:

<\s*br\s*\/?>   # matches <br> or <br /> (?=             # start of lookahead   .*?           # arbitrary text, lazy   (?:           # start of non-capturing group      (?!        # start of negative lookahead        <main>   # literal text <main>      )          # end of negative lookahead         [\s\s]*?   # match 0 or more of char including newline, lazy   )             # end of non-capturing group   <\/main>      # match </main> )               # end of lookahead /gi             # make global ignore case match 

regex demo


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -