Deprecated: Optional parameter $keys declared before required parameter $cms_id is implicitly treated as a required parameter in /home/www/dev/work/class/blog/CmsKey.php on line 75

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\main::$outlink is deprecated in /home/www/dev/work/website/lvesu/template/blog/cms/cms.tpl on line 2

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\main::$status is deprecated in /home/www/dev/work/website/lvesu/template/blog/index.head.php on line 2
解析HTML常用正则表达式 - 互联网笔记

略微加速

略速 - 互联网笔记

解析HTML常用正则表达式

2016-10-11 leiting (3584阅读)

标签 正则表达式

获取标签:

html.replace(/<(\/?)([\w\-]+)(>|(.*?[^\\])">|(.*?[^\\])'>)/gi, function (a, b, tagName, all, e, f)

{

     console.log(tagName);

     console.log(all);//标签后的文本

     console.log(e);//匹配">(不含)的文本

     console.log(f);//匹配'>(不含)的文本

     console.log('-----------------')

});


获取属性:

/([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi


清除html标签(排除img标签、换行、p标签):

html.replace(/<(?!img|p|\/p|(\/)?br)[^>]*>/gi, '');


清除非网络图片:

html.replace(/<img[^>]*src=[\"\'](?!http|https)[^>]*[\"\'][^>]*>/gi, '');


清除p、br上样式:

html.replace(/(<(?:p|br))\s*([^>]*)>/gi, "$1>");


北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3