最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript regex to accept only letters, spaces, and ñ - Stack Overflow

programmeradmin1浏览0评论

I am looking for a Javascript regex to make sure the string contains only spaces, letters, and ñ — case insensitively.

I already tried: /^[A-Za-z _]*[A-Za-z][A-Za-z _]*$/ but it fails to accept ñ.

I am looking for a Javascript regex to make sure the string contains only spaces, letters, and ñ — case insensitively.

I already tried: /^[A-Za-z _]*[A-Za-z][A-Za-z _]*$/ but it fails to accept ñ.

Share Improve this question edited Apr 20, 2016 at 17:18 Laurel 6,17314 gold badges33 silver badges59 bronze badges asked Sep 10, 2011 at 16:56 Jonathan EdgardoJonathan Edgardo 5131 gold badge9 silver badges23 bronze badges 6
  • I tried: /^[A-Za-z _]*[A-Za-z][A-Za-z _]*$/ - But this dont accept ñ Values. – Jonathan Edgardo Commented Sep 10, 2011 at 16:59
  • 1 Yes, because I just need a ASCII value in this case ñ – Jonathan Edgardo Commented Sep 10, 2011 at 17:02
  • 1 Just to be clear, that's not an ASCII character. These are. – Michael Petrotta Commented Sep 10, 2011 at 17:08
  • 3 The Spanish alphabet is not a-z plus ñ. Rather, it is [aábcdeéfghijklmnñoópqrstuúüvwxyzAÁBCDEÉFGHIJKLMNÑOÓPQRSTUÚÜVWXYZ] and furthermore, that will only work if you first run the data through Unicode Normalization Form C for Canonical Composition, normally called NFC. Otherwise your ñ character might and sometimes shall comprimise two separate code points: a normal n followed by U+0303 COMBINING TILDE. Unicode characters can have multiple code point representations. You need to normalize. – tchrist Commented Sep 10, 2011 at 19:03
  • 24 Shame shame on all you people for voting to close “for being too local”: that’s a terribly embarassing “ugly American” kind of attitude. Understanding how to deal with Unicode in regexes is something that even dumb monoglot anglophones need to do, whether you like it or else. Notice the curly quotes and apostrophes in this comment. Welcome to Cañon City, Colorado — and have a nice EM DASH sort of day! – tchrist Commented Sep 10, 2011 at 19:07
 |  Show 1 more comment

6 Answers 6

Reset to default 18
/^[ñA-Za-z _]*[ñA-Za-z][ñA-Za-z _]*$/

and

/^[\u00F1A-Za-z _]*[\u00F1A-Za-z][\u00F1A-Za-z _]*$/

should work.

Javascript regex supports \u0000 through \uFFFF.

If you simply want that caracter, insert it in the Regex, like [A-Za-zÑñ ]. Otherwise use a Unicode-knowing Regex library for Javascript like http://xregexp.com/. Sadly JS Regexes don't support Unicode compliant character classes (like \p{L} in C# regexes)

With this it forces not to have spaces at the beginning but if later

/^[a-zA-Z\u00C0-\u00FF][a-zA-Z\u00C0-\u00FF\s]*$/

..for email you can use:

/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i

..for password you can use:

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$/i

I hope it works for you like it did for me, good look..!

This worked out for me /^[a-zA-ZáéñóúüÁÉÑÓÚÜ -]*$/ a shorter version from @tchrist answer ^^

You need to use a character class.

/[A-Za-z ñ]+/

This works for me, allow utf8 characters like ñóíú and spaces

const validationsLetters = /^[a-zA-Z\u00C0-\u00FF ]*$/;
发布评论

评论列表(0)

  1. 暂无评论