简体   繁体   English

正则表达式,字符串是白名单字符的组合,不包含任何黑名单

[英]Regex, string is a combination of whitelisted characters and doesn't contain any in blacklist

1) How to regex validate user input to contain any combination of characters from group A and to not contain any characters from another group D? 1)如何正则表达式验证用户输入是否包含A组中的任何字符组合,以及不包含来自另一组D的任何字符?
2) Also check string length is between 2 and 255 2)同时检查字符串长度是否在2到255之间

In other words, for all string characters: A AND NOT D. 换句话说,对于所有字符串字符:A AND NOT D.

I receive two groups of characters (whitelist and blacklist) from a server and need to validate user input based on those. 我从服务器收到两组字符(白名单和黑名单),需要根据这些字符验证用户输入。 I cannot affect the design and must live with it. 我不能影响设计,必须忍受它。 I also must use regex because of other design restrictions. 由于其他设计限制,我也必须使用正则表达式。


Here's what I've got so far (not working at all): 这是我到目前为止所做的事情(根本不工作):

/^(?![23]+)[0-9]{2,255}$/

23 would be the blacklist of characters for simplicity 为简单起见, 23将是黑名单
0-9 would be the whitelist of characters for simplicity 为简单起见, 0-9将是字符的白名单

Some examples: 一些例子:

3014567890 --> fail, 3 is present 3014567890 - >失败,3存在
0145678902 --> fail, 2 is present 0145678902 - >失败,2存在
0123456789 --> fail, 2 and 3 are present 0123456789 - >失败,2和3存在
014567890 --> ok 014567890 - >好的
88774411489 --> ok 88774411489 - >好的
5 --> fail, not enough characters 5 - >失败,没有足够的人物
1abc --> fail, abc illegal chars 1abc - >失败,abc非法chars
ab1c --> fail, abc illegal chars ab1c - >失败,abc非法的chars
abc1 --> fail, abc illegal chars abc1 - >失败,abc非法的字符


Thanks! 谢谢!

You're nearly there, the lookahead assertion needs some work: 你几乎就在那里,先行断言需要一些工作:

/^(?!.*[23])[0-9]{2,255}$/

That way, the regex within the negative lookahead matches if there is (at least) one 2 or 3 anywhere in the string (ie, after any number of characters ( .* )), causing the assertion to fail. 这样,负向前瞻中的正则表达式匹配,如果字符串中有(至少)一个23 (即,在任意数量的字符( .* )之后),导致断言失败。

In this (apparently) simplified example, you could of course just have used /^[014-9]{2,255}$/ . 在这个(显然)简化的例子中,你当然可以使用/^[014-9]{2,255}$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript正则表达式-匹配一个包含一些字符但不包含其他字符的字符串 - JavaScript Regex - Match a string with that contains some characters and doesn't contain others 如何匹配字符串包含特定文本,前面是正则表达式中的字母数字和其他字符的任意组合 - How to match a string contain specific text preceded by any combination of alphanumerics and other charcters in regex 正则表达式,选择字符和区分大小写的任意组合 - Regex, any combination of select characters and case sensitive js-检查字符串是否不包含给定字符 - js - check if string doesn't contain given characters 不包含“图片”的字符串中网址的正则表达式 - Regex for URL within string that doesn't contain “images” 正则表达式中不能包含#或&的字符串 - regex for a string that can't contain # or & 从不符合正则表达式规则的字符串中删除字符 - Remove Characters from a String that doesn't comply with the Regex Rule 如何根据 JavaScript 中的白名单字符检查字符串? - How to check string against whitelisted characters in JavaScript? Javascript正则表达式匹配不包含关键字 - Javascript regex match doesn't contain keyword Javascript正则表达式匹配以某些字符结尾但不与这些字符的特定组合结束的字符串 - Javascript regex to match a string that ends with some characters but not with a particular combination of those
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM