Regex for names with special characters (Unicode)

前端 未结 7 2289
太阳男子
太阳男子 2020-11-28 09:28

Okay, I have read about regex all day now, and still don\'t understand it properly. What i\'m trying to do is validate a name, but the functions i can find for this on the i

7条回答
  •  天命终不由人
    2020-11-28 09:58

    When checking your input string you could

    • trim() it to remove leading/trailing whitespaces
    • match against [^\w\s] to detect non-word\non-whitespace characters
    • match against \s+ to get the number of word separators which equals to number of words + 1.

    However I'm not sure that the \w shorthand includes accented characters, but it should fall into "word characters" category.

提交回复
热议问题