I want to add functionality for user to add First name or Last name in any language for Eg. Spanish, French and Chinese.
Right now I am using this RegEx:
var firstNameRegex = /^[a-zA-Zu3000u3400-u4DBFu4E00-u9FFFu00C0-u017F-'s]{1,45}$/;
But this is only working for English Alphabets When I am trying to add French characters like
à- â - ä- é - è - ê - ë - ï - î - ô -ö - ù - û - ü- ÿ-ç
Then i am getting error.
Any suggestions.
2
Answers
Use this regex:
Or this if only whitespaces are not valid:
please see this regex cheatsheet
By using the regex unicode groups, you can test for all common used letters.
p{L}
orp{Letter}
contains the matching-information for all defined unicode symbols that represent a letter.For a list of the matching unicode symbols, you can take a look at https://www.compart.com/en/unicode/category. Everything in the categories starting with L, matches
p{L}
. These are Lowercase Letter, Modifier Letter, Other Letter, Titlecase Letter, Uppercase Letter.By using the modifier
u
for unicode, these groups (p{GroupName}
) will be parsed and you get the expected result.I would not recommend using the meta-character
s
in this use case, as it matches multiple white-space characters: space character (x20
), tab character (x09
), carriage return character (x0D
), new line character (x0A
), vertical tab character (x0B
), form feed character (x0C
)More information about the unicode groups can also be found on: