问题
Tried looking around for a regex pattern for a full name and just can't seem to find one.
Ideally would match like
Tom Franklin
tom Franklin
tom franklin
tom franklin-jones
i.e. allow 1 space in the middle and some basic hyphens etc but thats all. Does any one know how to do this ?
Edit: Including
René Hadron van der Ööps
回答1:
You can use /([\p{L}'-]+) ([\p{L}'-]+)/ to catch all possible letter. Or /([a-z'-]+) ([a-z'-]+)/i to catch only ascii letters.
The group ([\p{L}'-]+) means [\p{L}'-] repeated at least one time. And \p{L} means any unicode letter. The - at the end means that "-" is allowed in names.
Note :
When you capture characters with [] the hyphen must be either at the start of your characters set or at the end.
Edit :
Mr. O'Brien is happy now.
Resources :
- regular-expressions.info - unicode
- regular-expressions.info - character sets
回答2:
\w+\s+[a-zA-Z-]+
来源:https://stackoverflow.com/questions/3702737/regex-fullname-pattern