javascript - regular expression to validate accents, spaces and only letters - Stack Overflow

I am looking for a regular expression that validates accents, and that also does not allow characters d

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
Add a ment  | 

1 Answer 1

Reset to default 7

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744641970a4585495.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信