简体   繁体   English

正则表达式在反应原生中验证用户名

[英]Regex to validate a username in react native

I don't know much regex and when it comes to username it becomes a little tricky, so I request from my fellow programmers to help me validate a username with respect to certain conditions.我不太了解正则表达式,当涉及到用户名时,它变得有点棘手,所以我请求我的程序员同事帮助我根据某些条件验证用户名。

  1. Username can only contain letters, numbers, periods and underscores.用户名只能包含字母、数字、句点和下划线。
  2. Username can start and end with underscores but never with periods.用户名可以以下划线开头和结尾,但不能以句点开头。 (for security reasons) (出于安全原因)
  3. Username length should be between 4 and 20 characters.用户名长度应在 4 到 20 个字符之间。
  4. Spaces are not allowed不允许有空格

Examples of valid and invalid usernames:有效和无效的用户名示例:

josh valid乔希valid

.josh_ invalid .josh_ invalid

_josh. _乔什。 invalid

_josh_123.brad valid _josh_123.brad valid

josh brad invalid乔什布拉德invalid

I have already searched and the answer I find didn't quite help.我已经搜索过了,我找到的答案并没有太大帮助。 This is my regex for now:这是我现在的正则表达式:

RegExp('^(?=[A-Za-z0-9._]{4,20}$)[^_.].*[^_.]');

Thanks in advance提前致谢

This can be achieved without any lookahead:这可以在没有任何前瞻的情况下实现:

/^\w[\w.]{2,18}\w$/

If you want to use RegExp then use:如果你想使用RegExp然后使用:

var re = new RegExp("^\\w[\\w.]{2,18}\\w$");

RegEx Demo正则表达式演示

Pattern Details:图案详情:

  • \\w : is a shortcut for [a-zA-Z0-9_] \\w : 是[a-zA-Z0-9_]的快捷方式
  • ^ : Start ^ : 开始
  • \\w : Match a word character \\w : 匹配一个单词字符
  • [\\w.]{2,18} : Match 2 to 18 counts of word or dot characters thus making total length between 4 to 20 [\\w.]{2,18} :匹配 2 到 18 个单词或点字符,从而使总长度在 4 到 20 之间
  • \\w : Match a word character \\w : 匹配一个单词字符
  • $ : End $ : 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM