简体   繁体   中英

Javascript regex to accept only letters, spaces, and ñ

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

/^[ñA-Za-z _]*[ñA-Za-z][ñA-Za-z _]*$/

and

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

should work.

Javascript regex supports \ through \￿ .

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)

您需要使用字符类。

/[A-Za-z ñ]+/

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

这对我有用,允许使用 ñóíú 和空格等 utf8 字符

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

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

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