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.