简体   繁体   中英

JavaScript/PHP Regular Expression

I'm trying to match first names and Lastname with something like this.

$pattern = '/[a-zA-Z\-]{3,30} +[a-zA-Z]+/';

This works great, except when I have a first name like this Mélissa Smith

My match becomes Lissa Smith

How do I match for all special characters like é

in javascript, you can use a unicode char range instead of A-Za-z:

"Mélissa Smith".match(  /[\u80-\uffff]{3,30} +[\u80-\uffff]+/  )

equals: ["Mélissa Smith"]

Put the regex into Unicode mode with the /u modifier and use an appropriate Unicode character class instead of hardcoding just latin letters:

$pattern = '/^(\pL|-){3,30}\s+\pL+$/u';

I also anchored the pattern between ^ and $ because otherwise it could end up matching things you didn't intend it to.

You have to keep in mind that when you do this, the input (as well as the pattern itself) must be encoded in UTF-8.

However, it has to be said that naively parsing names like this is not going to give you very good results. People's full names are way too involved for something this simple to work across the board.

Try using the POSIX expression [:alpha:] instead of [a-zA-Z-] to catch the characters. [:alpha:] will catch equivalent characters such as accents.

http://www.regular-expressions.info/posixbrackets.html

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