简体   繁体   English

用于验证重音、空格和仅字母的正则表达式

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

相关问题 javascript中的正则表达式仅匹配字母或空格 - Regular expression in javascript to match only letters or spaces 只接受字母和空格的正则表达式 - Regular expression that ONLY accept letters and white spaces 正则表达式:仅限字母、数字、空格、连字符、句点和下划线,并且必须以字母开头 - Regular Expression: Letters, numbers, spaces, hyphens, periods, and underscores only and must begin with a letter 正则表达式:允许字母,数字和空格(至少有一个字母不是数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter not number) 正则表达式:允许字母,数字和空格(至少包含一个字母或数字) - Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number) 仅允许单词和空格的正则表达式 - regular expression for allowing words and spaces only 正则表达式,仅将名称与一个空格匹配 - regular expression to match name with only one spaces 正则表达式仅数字和空格 - Regular Expression only digits and blank spaces 什么是用于删除大写字母之间的空格的正则表达式,但在单词之间保留空格? - What is a regular expression for removing spaces between uppercase letters, but keeps spaces between words? 正则表达式只匹配字母,空格和非英语字符? - Regular expression to match only letters, space and non-English characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM