简体   繁体   English

javascript regex 需要至少一个字母,一个数字并防止添加某些单词

[英]javascript regex requiring at least one letter, one number and prevents from adding certain words

I'm trying to modify my regex which requires user to type at least one letter and one digit which looks like this :我正在尝试修改我的正则表达式,它要求用户至少输入一个字母和一个数字,如下所示:

new RegExp('^(?=.*[a-z])(?=.*[0-9]).+$')

And I want to prevent user from using certain words like email address part before @ .我想阻止用户在@之前使用某些词,例如电子邮件地址部分。

So let's assume the email address is example@example.com所以让我们假设电子邮件地址是example@example.com

I want to force user to use a string that doesn't contain example in it (any part of the string)我想强制用户使用不包含example的字符串(字符串的任何部分)

This is what I have so far:这是我到目前为止:

\b(?:(?!example)\w)+\b

But it doesn't really force the user to use at least one character and one digit.但它并没有真正强迫用户至少使用一个字符和一个数字。

When I'm trying to restric it I'm ending up with this:当我试图限制它时,我最终得到了这个:

\b(?:(?!example).*[a-z].*[0-9])+\b

But now the strings must follow the order of example then a [az] and then [0-9]但是现在字符串必须遵循example的顺序,然后是[az] ,然后是[0-9]

Any help greatly appreciated非常感谢任何帮助

Thanks!谢谢!

In your sample the negative lookahead disallows example only at start of the string.在您的示例中,负前瞻仅在字符串的开头不允许example Just add .*?只需添加.*? as joker to disallow the word anywhere in the string and use word boundaries \\b if needed.作为小丑禁止在字符串中的任何位置使用单词,并在需要时使用单词边界\\b

/^(?!.*?example)(?=\D*\d)[^a-z]*[a-z].*$/i
  • (?!.*?example) first lookahead disallows example anywhere in the line (?!.*?example) first lookahead 不允许该行中的任何位置的example
  • (?=\\D*\\d) second lookahead requires a digit after any amount of \\D non-digits. (?=\\D*\\d)第二次前瞻需要在任意数量的\\D非数字之后有一个数字。
  • [^az]*[az] matches any amount of non -alphetic characters until an alphabetic. [^az]*[az]匹配任意数量的字母字符直到字母。

See demo at regex101在 regex101 上查看演示

Actually you just need two lookaheads for being independent of condition.实际上,您只需要两个前瞻即可独立于条件。 One for the required digit and one for the word.一个用于所需的数字,一个用于单词。 The requirement of alphabetic can be done inside the pattern.字母的要求可以在模式内完成。

Lookarounds are zero-length assertions triggered at a certain position.环视是在某个位置触发的零长度断言。

暂无
暂无

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

相关问题 Javascript正则表达式至少匹配一个字母或数字? - Javascript regex matching at least one letter or number? javascript:使用RegEx允许字母,数字和空格(至少包含一个字母和数字) - javascript: Use RegEx to allow letters, numbers, and spaces (with at least one letter and number) 正则表达式:必须至少包含一个数字和字母,但不能包含其他字符/空格 - RegEx: Must have at least one number and letter but no other characters / whitespace 长度为N的字符串包含至少一个字母x的javascript正则表达式 - javascript regex for string of length N that contains at least one letter x in it 正则表达式在 JavaScript 中包含至少一个字母和至少 6 个字符长的所有字符 - Regex to include all characters with at least one letter and at least 6 characters long in JavaScript 正则表达式:必须至少有一个数字、一个字母并且应该允许空格 - RegEx: Must have at least one number, one letter and should allow whitespaces 最少 6 个字符的密码 REGEX,至少一个字母和一个数字,可以包含特殊字符 - Password REGEX with min 6 chars, at least one letter and one number and may contain special characters 一个字母和三个数字的正则表达式 - regex for one letter and three number 至少匹配一个字母,一个数字和一个非字母数字 - Match at least one letter, one number and one non-alphanum JavaScript:正则表达式为一个字母和一个点 - JavaScript: Regex for one letter and a point
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM