donglu7286
donglu7286
2014-04-06 15:19
浏览 87
已采纳

正则表达式匹配所有结束HTML标记

For some reason, I need to remove all closing HTML tags from a string in PHP, so I need a Regular Expression to match all closing HTML Tags. I'm extremely unexperienced with Regular Expressions, so I tried using /\<\\.*\>/g and /<\\.*>/g, but that didn't work. What should I use instead?

Please note that I only want to match closing HTML tags. Opening HTML Tags should remain untouched. Thanks in advance!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douhui2307
    douhui2307 2014-04-06 15:21
    已采纳

    Use this regex:

    </.+?>
    

    or

    /<\/.+?>/
    

    That will do.

    Live Demo

    点赞 评论
  • dousi5501
    dousi5501 2014-04-06 15:25

    Matching HTML with a regex is not a good idea, but if you have to, you can use

    /<\/[^<>]*>/g
    

    Explanation:

    </     # Match </
    [^<>]* # Match any number of characters except angle brackets
    >      # Match >
    
    点赞 评论
  • dtyz76562
    dtyz76562 2014-04-06 20:18
    /<\/[\w^_]*>/
    

    because u dont want to match ?.=- and _ in tag.

    点赞 评论

相关推荐