且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

RegEx拒绝未转义的HTML字符

更新时间:2022-06-10 22:44:59

此正则表达式匹配任何& 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 &:

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

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.