16

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 ñ.

6
  • I tried: /^[A-Za-z _]*[A-Za-z][A-Za-z _]*$/ - But this dont accept ñ Values. Sep 10, 2011 at 16:59
  • 1
    Yes, because I just need a ASCII value in this case ñ Sep 10, 2011 at 17:02
  • 1
    Just to be clear, that's not an ASCII character. These are. 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
    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
    Sep 10, 2011 at 19:07

6 Answers 6

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.

2
  • Your response helped me a lot! Thanks Sep 10, 2011 at 17:14
  • 4
    That’s fundamentally flawed in that you didn’t tell him to run his data through an NFC conversion first. If he doesn’t, those patterns don’t work. I’m pretty sure he really wants [aábcdeéfghijklmnñoópqrstuúüvwxyzAÁBCDEÉFGHIJKLMNÑOÓPQRSTUÚÜVWXYZ] for the Spanish alphabet, but that will only work if you NFC the data!!
    – tchrist
    Sep 10, 2011 at 19:01
5

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)

2
  • Does C# actually make you write \p{L} instead of just \pL????? That's really dumb, because it triples the length of the specifier for the 7 most common cases. That's a Huffman failure. Somebody wasn't very smart. You need to make \pL only a little bit harder to type than \w. Otherwise people won’t use it. \p{L} is a design bug.
    – tchrist
    Sep 10, 2011 at 18:43
  • @tchrist BUT in the end it less than double the length of the "expression". \p{L} is 5 chars and \pL is 3. Yeah, \pL would be better, but I DO think that supporting non-BMP chars and the \X would be MUCH more important :-)
    – xanatos
    Sep 11, 2011 at 6:37
1

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..!

1

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

0

You need to use a character class.

/[A-Za-z ñ]+/
0
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.