正则表达式排除某些标签 [英] regex exclude certain tags

查看:42
本文介绍了正则表达式排除某些标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需要一个快速的帮助来解决这个问题.

just need a quick help for solving this problem.

我想从一个字符串中去除所有 html 标签,除了白名单(变量)中的标签.

I want to strip all html tags out of a string except the tags from a whitelist(variable).

到目前为止我的代码:

whitelist = 'p|br|ul|li|strike|em|strong|a', 
reqExp = new RegExp('<\/?[^>|' + whitelist + ']+\/?>');

问题是现在它或多或少地工作得很好,但也没有删除例如 b 因为它与 br 中的 b 匹配的白名单.

The problem is now it works more or less fine but also not removing for example b because it matches the b from the br out of the whitelist.

我尝试了不同的方法,但没有找到正确的解决方案.我怎么能告诉正则表达式做类似 /.WITHOUT(smth)/ 的事情(因此:匹配所有期待接下来的一切).

I tried different approaches but dont find the right solution. How can i tell the regex to do something like /.WITHOUT(smth)/ (therefore: match all expect everything following).

推荐答案

使用这个正则表达式:-

Use this regex:-

<(?!/?(p|br|ul|li|strike|em|strong|a)(>|\s))[^<]+?>

现场演示

有关详细信息,请参阅我之前的回答,它满足您的要求.

LIVE DEMO

For more information, refer to my earlier answer, which fullfill your requirement.

这篇关于正则表达式排除某些标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆