Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

RegEx - match specific html tags between other tag

I′ve got a problem that I have to remove all HTML br tags in an unordered list. The input HTML looks like this

<br /><ul><br /><li>one</li><br /><li>two</li><br /></ul><br />

Now I have to remove the br tags in the unordered list (and not the ones before and following)

I tried to solve the problem via regular expressions

(?<=(<ul.*>))(.*)(?=(</ul>))

That gives me the text between the ul tags.

But how can I get only the br tags in the selection?

https://regexr.com/5jo2s

Thank you very much in advance!

question from:https://stackoverflow.com/questions/65600457/regex-match-specific-html-tags-between-other-tag

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution from Wiktor Stribizew (see above) is working

/(?<=<ul[^>]*>.*?)<br />(?=.*?</ul>)/g


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...