使用正则表达式测试多种语言的字符

时间:2017-04-17 21:39:38

标签: javascript regex internationalization

对于我的网站,我正在为用户植入用户名。我正在查看Snapchat的用户名验证规则,以指导我获得一些最佳做法,他们有以下内容:

Must be 3-15 characters long
Can’t contain spaces
Must begin with a letter
Can only contain letters, numbers, and the special characters hyphen ( - ), underscore ( _ ), and period ( . ), EXCEPT that the username:
Can’t begin with a number, hyphen, underscore, or period
Can’t end with a hyphen, underscore, or period
Can’t contain emojis or other symbols such as @, $, #, etc.
Will appear only in lower-case letters within the app

这让我想到这是如何适用于全世界的用户的。当Must begin with a letter之类的要求如何使用正则表达式检查时,所有语言中的所有字母都会通过,但同时会出现诸如表情符号或“怪异”等字样。人物失败了?

1 个答案:

答案 0 :(得分:0)

您可能需要以下内容:

(?=[\p{L}\d_\-.]{3,13})^[\p{L}](?=.*?[^ ])[\p{L}\d_\-.]+[\p{L}\d]$

Regex Demo