[英]Angularjs regular expression
In my Angularjs application I need to have regular expression for two patterns for form validation with the following condition. 在我的Angularjs应用程序中,我需要有两个模式的正则表达式,用于表单验证,具有以下条件。
Pattern 1 : 模式1:
The input box should accept alphanumeric with no space and it should also allow the user to use the characters like ~!@#$-_
any where in the string, except these characters non of the other characters should be allowed like (%, &, ^ etc)
. 输入框应该接受没有空格的字母数字,并且它还应该允许用户使用字符串中的
~!@#$-_
any字符,除了这些字符之外应该允许其他字符(%, &, ^ etc)
。 It should not allow leading/trailing whitespace also. 它也不应该允许前导/尾随空格。
Examples : 例子 :
ab@4_w : valid
sd!tye123 : valid
sd%tye123 : Invalid
sd*tye123 : Invalid
$scope.pattern1 = [\\w~!@#\\$-]+ $ scope.pattern1 = [\\ w〜!@#\\ $ - ] +
Pattern 2: Should allow only alphanumeric with no space and no other characters including (_). 模式2:应该只允许没有空格的字母数字,也不允许包括(_)在内的其他字符。 It should not allow leading/trailing whitespace also.
它也不应该允许前导/尾随空格。
Examples : 例子 :
a4hgg5 : Valid
a4_6hy : Invalid
a@yb : invalid
$scope.pattern2 = [\\w]+ $ scope.pattern2 = [\\ w] +
$scope.pattern1
and $scope.pattern2
needs to be modified to meet my above requirements. 需要修改
$scope.pattern1
和$scope.pattern2
以满足我的上述要求。
Try 尝试
^[\w~!@#\$-]+$
^
Begin of string ^
字符串的开头 [\\w~!@#\\$-]+
Any number of the Characters you want, (note the need to escape $
like \\$
) [\\w~!@#\\$-]+
任意数量的您想要的人物,(注意,需要逃避$
像\\$
) $
End of string $
字符串结束 See in Action on Regex101 . 请参阅Regex101上的Action 。 If you want empty strings to be valid, specify
*
instead of +
for the quantifier. 如果要使空字符串有效,请为量词指定
*
而不是+
。 Also, when using \\w
you do not need to set the i
flag, since it already covers both upper and lowercase letters. 此外,使用
\\w
您不需要设置i
标志,因为它已经覆盖了大写和小写字母。
It should not allow leading/trailing whitespace.
它不应该允许前导/尾随空格。
In both cases, add the ng-trim="false"
attribute to the input
element. 在这两种情况下,将
ng-trim="false"
属性添加到input
元素。
The input box should accept alphanumeric with no space and it should also allow the user to use the characters like
~!@#$-_
any where in the string, except these characters non of the other characters should be allowed like (%
,&
,^
etc).输入框应该接受没有空格的字母数字,并且它还应该允许用户使用字符串中的
~!@#$-_
any字符,除了这些字符之外应该允许其他字符(%
,&
,^
等)。
The pattern you have is correct, but escaping characters that do not have to be escaped is not recommended, use: 您拥有的模式是正确的,但不建议转义不必转义的字符,请使用:
^[\w~!@#$-]+$
Where \\w
matches [a-zA-Z0-9_]
. \\w
匹配[a-zA-Z0-9_]
。
NOTE: if you pass the pattern as a string, do not add ^
and $
anchors, but double the backslashes: "[\\\\w~!@#$-]+"
. 注意:如果您将模式作为字符串传递,请不要添加
^
和$
anchors,而是加倍反斜杠: "[\\\\w~!@#$-]+"
。
Should allow only alphanumeric with no space and no other characters including (
_
).应该只允许没有空格的字母数字,也不允许包括(
_
)在内的其他字符。
It is much easier: ^[a-zA-Z]+$
. 它更容易:
^[a-zA-Z]+$
。 Same comment about anchors as above applies. 关于上述锚点的相同评论适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.