[英]Regex acts weird
I want to create a regex expression that I can pass to ng-pattern so that the input can only be valid if the string has only uppercase/lowercase latin letters from a to z, n with tilde, and vowels with acute accents; 我想创建一个可以传递给ng-pattern的正则表达式,以便仅当字符串中只有从a到z的大写/小写拉丁字母,带有波浪号的n和带有尖音的元音时,输入才有效。 as well as dot.
以及点。 I came up with what I thought was the solution, but angularjs keeps telling me in the developer tools that my string is not valid when string is:
我提出了我认为是解决方案的方法,但是angularjs不断在开发人员工具中告诉我,当string为:
This is what I've got : "[A-Za-z\\.\\s\\U+00C1\\U+00C9\\U+00CD\\U+00D1\\U+00D3\\U+00DA\\U+00E1\\U+00E9\\U+00ED\\U+00F1\\U+00F3\\U+00FA]+"
这就是我得到的:
"[A-Za-z\\.\\s\\U+00C1\\U+00C9\\U+00CD\\U+00D1\\U+00D3\\U+00DA\\U+00E1\\U+00E9\\U+00ED\\U+00F1\\U+00F3\\U+00FA]+"
What am I doing wrong? 我究竟做错了什么?
PS I tried the |
PS我尝试了
|
operator mentioned in the wiki for the [Jun|Jul|Aug]
example, but it acts even weirder. Wiki在
[Jun|Jul|Aug]
示例中提到了运算符,但它的作用甚至更怪异。
此正则表达式应满足您的需求:
[A-Za-z\.\s\u00C1\u00C9\u00CD\u00D1\u00D3\u00DA\u00E1\u00E9\u00ED\u00F1\u00F3\u00FA]+
You should use a case-insensitive regular expression (the i
flag) and escape your special characters with \\xHH
instead of \\u+00HH
. 您应该使用不区分大小写的正则表达式(
i
标志),并使用\\xHH
而不是\\u+00HH
来转义特殊字符。
var regex = /^[az.\\s\\xC1\\xC9\\xCD\\xD1\\xD3\\xDA\\xE1\\xE9\\xED\\xF1\\xF3\\xFA]+$/i var valid = 'ñ Ñ á e í ó ú'.split(' ') var invalid = '0 ; Ć'.split(' ') console.log('Valid:') valid.forEach(function (e) { console.log(regex.test(e)) //=> true }) console.log('Invalid:') invalid.forEach(function (e) { console.log(regex.test(e)) //=> false })
.as-console-wrapper { min-height: 100vh; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.