[英]regular expression to validate accents, spaces and only letters
I am looking for a regular expression that validates accents, and that also does not allow characters different from accents and allows spaces but not characters other than letters.我正在寻找一个验证重音的正则表达式,它也不允许与重音不同的字符,并允许空格但不允许除字母以外的字符。 I currently use this but I get an error for the spaces.
我目前使用它,但我收到空格错误。 How can I fix it?
我该如何解决?
function cambiarNombre(nombre){
let regex = /^[a-zA-ZÀ-ÿ\u00f1\u00d1]+(\s*[a-zA-ZÀ-ÿ\u00f1\u00d1]*)*[a-zA-
ZÀ-ÿ\u00f1\u00d1]+$/g;
return regex.exec(nombre)[0];
}
console.log(cambiarNombre("UNA palabra ñoÑerías ")); //true
console.log(cambiarNombre("UNA palabra ñoÑerías*")); //false *
console.log(cambiarNombre("UN2A palabra1 5oÑerías")); //false 2 1 5
console.log(cambiarNombre("palabra2")); //false 2
console.log(cambiarNombre(" palabra2")); //false 2
console.log(cambiarNombre(" pálabña ")); //true
console.log(cambiarNombre("juan perez")); //true
console.log(cambiarNombre("juan pérez")); //true
console.log(cambiarNombre("juan")); //true
console.log(cambiarNombre("júan")); //true
allow accents, spaces and letters.允许重音、空格和字母。 Thank you
谢谢
To match letters, accents and Spaces everywhere, except Spaces at start, you can use the following regex (which is actually simpler than the one you have):要匹配任何地方的字母、重音符号和空格,除了开头的空格,您可以使用以下正则表达式(实际上比您拥有的更简单):
/^[ a-zA-ZÀ-ÿ\u00f1\u00d1]*$/g
This will select all the different letters at start, the same + Space
for the following.这将在开始时选择所有不同的字母,以下相同+
Space
。
Note the space
in the pattern, if you want to match all White Space
, you can use \\s
instead,注意模式中的
space
,如果你想匹配所有的White Space
,你可以使用\\s
代替,
Edit :编辑:
Now Spaces are allowed at any position but NOT required.现在可以在任何位置使用空格,但不是必需的。 The only requirement is that all characters must be one of the characters in the Square brackets repeated zero or more times.
唯一的要求是所有字符必须是方括号中重复零次或多次的字符之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.