简体   繁体   中英

Remove only anchor tags from string with regex

I know that to remove all html tags from a string one can use:

string = re.sub('<[^<]*?/?>', '', string)

But is there anyway that I can remove only anchor tags and keep all other tags. So for example:

<p>Some text<a href="#">link</a></p>

become:

<p>Some text link</p>

It's enough to look for opening and closing a tags separately and omit them:

<(?:a\b[^>]*>|/a>)

Live demo

Thanks revo it worked perfectly. I also manage to fix this problem using this regex

string = re.sub('<a.*?>|</a> ', '', string)

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