Strip all HTML tags except links

后端 未结 6 924
小蘑菇
小蘑菇 2020-11-29 03:29

I am trying to write a regular expression to strip all HTML with the exception of links (the and tags respectively. It does n

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 04:15

    <(?!\/?a(?=>|\s.*>))\/?.*?>
    

    Try this. Had something similar for p tags. Worked for them so don't see why not. Uses negative lookahead to check that it doesn't match a (prefixed with an optional / character) where (using positive lookahead) a (with optional / prefix) is followed by a > or a space, stuff and then >. This then matches up until the next > character. Put this in a subst with

    s/<(?!\/?a(?=>|\s.*>))\/?.*?>//g;
    

    This should leave only the opening and closing a tags

提交回复
热议问题