admin管理员组文章数量:1393310
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
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
Share Improve this question edited Sep 21, 2020 at 23:04 Poul Bak 11k5 gold badges38 silver badges69 bronze badges asked Sep 24, 2018 at 21:59 yavgyavg 3,11112 gold badges53 silver badges135 bronze badges 4- Give an example input and expected output. – Poul Bak Commented Sep 24, 2018 at 22:35
- Your regex only allow Spaces in the middle, is that on purpose? – Poul Bak Commented Sep 24, 2018 at 22:40
- @PoulBak I updated the question, basically I should allow spaces, accents, and letters. – yavg Commented Sep 24, 2018 at 22:46
- Ok, Spaces at the end, in the middle, but not at the start, is this correct? – Poul Bak Commented Sep 24, 2018 at 22:47
1 Answer
Reset to default 7To 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.
Note the space
in the pattern, if you want to match all White Space
, you can use \s
instead,
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.
本文标签: javascriptregular expression to validate accentsspaces and only lettersStack Overflow
版权声明:本文标题:javascript - regular expression to validate accents, spaces and only letters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744641969a2617176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论