简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM