简体   繁体   English

我如何编写 Javascript 正则表达式模式来处理这些情况

[英]How can I write the Javascript Regular Expression pattern to handle these conditions

In My exercise, I'm given a task to write a regular expression pattern that validates username input for storage into database.在我的练习中,我的任务是编写一个正则表达式模式来验证用户名输入以存储到数据库中。

Usernames can only use alpha-numeric characters.用户名只能使用字母数字字符。

The only numbers in the username have to be at the end.用户名中唯一的数字必须在末尾。 There can be zero or more of them at the end.最后可以有零个或多个。 Username cannot start with the number.用户名不能以数字开头。

Username letters can be lowercase and uppercase.用户名字母可以是小写和大写。

Usernames have to be at least two characters long.用户名的长度必须至少为两个字符。 A two-character username can only use alphabet letters as characters.两个字符的用户名只能使用字母作为字符。

I succeeded to pass all tests except one我成功通过了除一项以外的所有测试

A1 is not supposed to match the patern A1 不应该匹配模式

let userCheck = /^[A-Za-z]+\w\d*$/;
let result = userCheck.test(username);

Edit:编辑:

My first answer did not fully solve the task.我的第一个答案没有完全解决任务。 This regex does:这个正则表达式做:

^([A-Za-z]{2}|[A-Za-z]\w{2,})$

it matches either two characters, or one character followed by at least two characters and/or digits ( \w == [A-Za-z0-9] ).它匹配两个字符,或一个字符后跟至少两个字符和/或数字( \w == [A-Za-z0-9] )。 See the demo here: https://regex101.com/r/sh6UpX/1在此处查看演示: https://regex101.com/r/sh6UpX/1

First answer (incorrect)第一个答案(错误)

This works for your description:这适用于您的描述:

let userCheck = /^[A-Za-z]{2,}\d*$/;
let result = userCheck.test(username);

Let me explain what went wrong in your regex:让我解释一下你的正则表达式出了什么问题:

/^[A-Za-z]+\w\d*$/

You correctly match, that the first character is only a letter.你正确匹配,第一个字符只是一个字母。 The '+' however only ensures, that it is matched at least one time.然而,'+' 只能确保它至少匹配一次。 If you want to match something an exact number of times, you can append '{x}' to your match-case.如果你想匹配某些东西的确切次数,你可以 append '{x}' 到你的匹配案例。 If you rather want to match a minimum and maximum amount of times, you can append '{min, max}'.如果您想匹配最小和最大次数,可以使用 append '{min, max}'。 In your case, you only have a lower limit (2 times), so the max stays empty and means unlimited times: {2,}在你的情况下,你只有一个下限(2 次),所以最大值保持为空,意味着无限次:{2,}

After your [2 - unlimited] letters, you want to have [0 - unlimited] numbers.在你的 [2 - unlimited] 个字母之后,你想要 [0 - unlimited] 个数字。 '\w' also matches letters, so we can just remove it. '\w' 也匹配字母,所以我们可以删除它。 The end of your regex was correct, as '\d' matches any digit, and '*' quantifies the previous match for the range [0 - unlimited].正则表达式的末尾是正确的,因为 '\d' 匹配任何数字,而 '*' 量化范围 [0 - 无限制] 的前一个匹配项。

I recommend using regex101.com for testing and developing regex patterns.我建议使用 regex101.com 来测试和开发正则表达式模式。 You can test your strings and get very good documentation and explanation about all the tags.您可以测试您的字符串并获得关于所有标签的非常好的文档和解释。 I added my regex with some example strings to this link: https://regex101.com/r/qPmwhG/1我将带有一些示例字符串的正则表达式添加到此链接: https://regex101.com/r/qPmwhG/1

The strings that match will be highlighted, the others stay without highlighting.匹配的字符串将被突出显示,其他字符串不突出显示。

You can use an alternation after ^[az] the first letter to require either [az]+ one or more letters followed by \d* any amount of digits |您可以在第一个字母^[az]之后使用交替来要求[az]+一个或多个字母后跟\d*任意数量的数字| OR \d{2,} two or more digits up to $ end of the string.\d{2,}两个或更多数字,直到字符串的$结尾

let userCheck = /^[a-z](?:[a-z]+\d*|\d{2,})$/i;

See this demo at regex101 - Used with the i -flag (ignore case) to shorten [A-Za-z] to [az] .请参阅 regex101 中的此演示- 与i -标志(忽略大小写)一起使用以将[A-Za-z]缩短为[az]


PS : Just updated my answer at some late cup of coffee ☕. PS :刚刚在一些迟来的咖啡☕更新了我的答案。 Had previously misread the question and removed my answer in meanwhile.之前误读了问题并同时删除了我的答案。 I would also have missed the part with eg Z97 which I just read at the other answers comments.我也会错过我刚刚在其他答案评论中读到的例如Z97的部分。 It's much more of a challenge than at first glance... obviously:)这比乍一看更具挑战性......显然:)

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

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