简体   繁体   English

用于防止字符串中出现 5 个或更多特殊字符的正则表达式模式

[英]RegEx Pattern for preventing 5 or more special characters in a string

Want a pattern in HTML input field to not accept string with 5 or more consecutive special symbol (.@#$%^&*).希望 HTML 输入字段中的模式不接受具有 5 个或更多连续特殊符号 (.@#$%^&*) 的字符串。

"abc@@##xy" : accept
"@@##!" : reject
"abc" : accept
"abc@@##@xy" : reject
"abc@@##xy@@##" : accept
"abc@@##xy@@##@" : reject

You could use a negative lookahead with a .您可以使用带有. to prevent the appearance of 5 consecutive special chars in the string with防止在字符串中出现 5 个连续的特殊字符

/^(?:(?![!@#$%^&*]{5,}).)*$/

[,@#$%^&*]{5,} matches 5 or more of the characters ! [,@#$%^&*]{5,}匹配 5 个或更多字符! , @ , # , $ , % , ^ , & , * (consecutively) , @ , # , $ , % , ^ , & , * (连续)

(?,[!@#$%^&*]{5,}) adds a negative lookahead to find a places in the string not followed by 5 consecutive 'special' characters (?,[!@#$%^&*]{5,})添加负前瞻以查找字符串中不跟随 5 个连续“特殊”字符的位置

adding a .添加一个. with a * quantifier matches any number of characters after such a position in the string, while the ^ and $ limit it to the beginning and end of a single line*量词的匹配字符串中此类 position 之后的任意数量的字符,而^$将其限制为单行的开头和结尾


regex101正则表达式101

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM