略微加速

略速 - 互联网笔记

解析HTML常用正则表达式

2016-10-11 leiting (2755阅读)

标签 正则表达式

获取标签:

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