9 Regular Expressions to strip HTML tags

3 篇文章 0 订阅

原文链接:http://www.pagecolumn.com/tool/all_about_html_tags.htm


It's not an easy job to parse HTML tags of the whole page using regular expressions. But if you are dealing with a part of HTML tags and handle it as a string, the following regular expressions may be of your help.

1
matches specific tag pairs and content between them
RegEx Expression:
/<\s*h4[^>]*>(.*?)<\s*/\s*h4>/g
Method:
exec, match
Testing String
<h4 class="sds">And more ...</h4>

2
matches all HTML tags pairs including attributes in the tags
RegEx Expression:
/<(.|\n)*?>/g
Method:
match
Testing String
<div class="tab0">CSS code formatter</div><div class="tab2">CSS code compressor</div>
3
match all start tags including attributes in the tags
RegEx Expression:
/<\s*\w.*?>/g
Method:
match
Testing String
<div class="box">5 px radius of round corner</div><div class="box">7 px radius of round corner</div><div style="color:#6699cc">color</div>
4
matches all close tag
RegEx Expression:
/<\s*\/\s*\w\s*.*?>|<\s*br\s*>/g
Method:
match
Testing String
<div class="sds">not sure where it can be used</div></br>
5
matches start tag of specific tag including attibutes
RegEx Expression:
/<\s*div.*?>/g
Method:
match
Testing String
<div class="tab1">tabs generator</div>
6
matches close part of specific tag pair
RegEx Expression:
/<\s*\/\s*div\s*.*?>/g
Method:
match
Testing String
<div class="sds">javascript + CSS ...</div>
7
matches specific HTML tag pair including attributes in the tags.
RegEx Expression:
/<\s*\/?\s*span\s*.*?>/g
Method:
match
Testing String
<span class="csc">Regex examples</span>
8
matches start tag with specific attribute
RegEx Expression:
/<\s*\w*\s*style.*?>/g
Method:
match
Testing String
<div style="color:#6699cc">round corner</div>
9
matches start tag with specific attribute
RegEx Expression:
/<\s*\w*\s*href\s*=\s*"?\s*([\w\s%#\/\.;:_-]*)\s*"?.*?>/g
Method:
exec, match
Testing String
<span ><a href="http://www.pagecolumn.com/"> 3 Column Layout Generator </a></span> <span > <a href="http://www.pagecolumn.com/2_col_generator.htm">2 Column Layout Generator</a></span
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值