Regex检查第一个字符是否大写。

时间:2022-08-09 20:13:40

I'm trying to check that the first character of a username is capital, the following can be letters or numbers and at most 20 characters long. Can someone explain why my syntax is wrong?

我试着检查用户名的第一个字符是大写,下面可以是字母或数字,最多20个字符。有人能解释为什么我的语法错误吗?

/^[A-z][a-z0-9_-]{3,19}$/

5 个解决方案

#1


23  

Your first Z is not a capital Z.

第一个Z不是大写的Z。

/^[A-Z][a-z0-9_-]{3,19}$/

#2


5  

Your first character needs to be A-Z, not A-z

你的第一个角色需要是A-Z,而不是A-Z。

So

所以

/^[A-z][a-z0-9_-]{3,19}$/

/ ^[a - z][a-z0-9_ -]{ 3 19 } /美元

Should be

应该是

/^[A-Z][a-z0-9_-]{3,19}$/

/ ^[a - z][a-z0-9_ -]{ 3 19 } /美元

#3


5  

I would do it like this:

我会这样做:

var firstChar = strToCheck.substring(0, 1);

if (firstChar == firstChar.toUpperCase()) {
    // it is capital :D
}

#4


5  

Why can't you let the poor users pick their own usernames? What you should do is convert all caps to lowercase.

为什么不能让可怜的用户选择自己的用户名呢?你应该做的是把所有大写字母转换成小写字母。

"User Name".toLowerCase();

But if you are truly evil, you should change that z to a Z:

但是如果你真的是邪恶的,你应该把z改成z:

/^[A-Z][A-Za-z0-9_-]{3,19}$/

#5


0  

You have a typo, the first z should be a capital -

你打错了,第一个z应该是大写的。

/^[A-Z][a-z0-9_-]{3,19}$/

#1


23  

Your first Z is not a capital Z.

第一个Z不是大写的Z。

/^[A-Z][a-z0-9_-]{3,19}$/

#2


5  

Your first character needs to be A-Z, not A-z

你的第一个角色需要是A-Z,而不是A-Z。

So

所以

/^[A-z][a-z0-9_-]{3,19}$/

/ ^[a - z][a-z0-9_ -]{ 3 19 } /美元

Should be

应该是

/^[A-Z][a-z0-9_-]{3,19}$/

/ ^[a - z][a-z0-9_ -]{ 3 19 } /美元

#3


5  

I would do it like this:

我会这样做:

var firstChar = strToCheck.substring(0, 1);

if (firstChar == firstChar.toUpperCase()) {
    // it is capital :D
}

#4


5  

Why can't you let the poor users pick their own usernames? What you should do is convert all caps to lowercase.

为什么不能让可怜的用户选择自己的用户名呢?你应该做的是把所有大写字母转换成小写字母。

"User Name".toLowerCase();

But if you are truly evil, you should change that z to a Z:

但是如果你真的是邪恶的,你应该把z改成z:

/^[A-Z][A-Za-z0-9_-]{3,19}$/

#5


0  

You have a typo, the first z should be a capital -

你打错了,第一个z应该是大写的。

/^[A-Z][a-z0-9_-]{3,19}$/