RegEx拒绝未转义的HTML字符 [英] RegEx to Reject Unescaped HTML Character

查看:249
本文介绍了RegEx拒绝未转义的HTML字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制特定输入字段中未转义的&符号的使用。我无法获得RegEx以杀死&的使用除非是amp,否则或者只是限制使用&(注意空格)。

I want to restrict usage of unescaped ampersands in a particular input field. I'm having trouble getting a RegEx to kill usage of "&" unless followed by "amp;"...or perhaps just restrict usage of "& " (note the space).

我试图调整答案在此线程中 ,但无济于事。谢谢。

I tried to adapt the answer in this thread, but to no avail. Thanks.

(FWIW,这里是一个RegEx,我确保一个文件名字段不包含有限的字符,并以.mp3结尾,它工作正常,但是它看起来效率高吗?)

(FWIW, here's a RegEx I made to ensure that a filename field didn't contain restrited chars. and ended in .mp3. It works fine, but does it look efficient?)

^[^&,<,>,:,",/,\\,|,?,\*]+(\.mp3|\.MP3|\.Mp3|\.mP3)$


推荐答案

此正则表达式匹配任何& code> amp; :

This regular expression matches any occurrence of & which is not followed by amp;:

/&(?!amp;)/

Rubulate

此正则表达式接受包含除& 之外的字符的字符串,或者字符串& amp;

This regular expression accepts strings that contain characters except &, or the string &amp;:

/^([^&]|&amp;)*$/

Rubular

您可以使用其中一个或另一个,取决于哪个是最方便的,不同之处在于如果第一个正则表达式匹配,该字符串应该被拒绝,而如果第二个正则表达式匹配,则该字符串应该被接受

You can use either one or the other, depending on which is most convenient. The difference is that the string should be rejected if the first regular expression matches, whereas the string should be accepted if the second regular expression matches.

这篇关于RegEx拒绝未转义的HTML字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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