[英]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.