正则表达式(第一个字符必须是a-z)

时间:2022-02-18 21:15:44

I have this regex: /[^a-zA-Z0-9_-]/

我有这个正则表达式:/[^ a-zA-Z0-9_ -]/

What I want to add to above is:

我想补充的是:

  • first character can be only a-zA-Z
  • 第一个字符只能是a-zA-Z

How I could make this regular expression?

我怎么才能写出这个正则表达式呢?

2 个解决方案

#1


28  

Try something like this:

试试这样:

^[a-zA-Z][a-zA-Z0-9.,$;]+$

Explanation:

解释:

^                Start of line/string.
[a-zA-Z]         Character is in a-z or A-Z.
[a-zA-Z0-9.,$;]  Alphanumeric or `.` or `,` or `$` or `;`.
+                One or more of the previous token (change to * for zero or more).
$                End of line/string.

#2


7  

I think this would also work

我想这也可以

^[a-zA-Z].*

If you wanted to test just the first character as being alphabetical and the rest of the string can be anything.

如果您想要测试第一个字符是按字母顺序排列的,其余的字符串可以是任何东西。

#1


28  

Try something like this:

试试这样:

^[a-zA-Z][a-zA-Z0-9.,$;]+$

Explanation:

解释:

^                Start of line/string.
[a-zA-Z]         Character is in a-z or A-Z.
[a-zA-Z0-9.,$;]  Alphanumeric or `.` or `,` or `$` or `;`.
+                One or more of the previous token (change to * for zero or more).
$                End of line/string.

#2


7  

I think this would also work

我想这也可以

^[a-zA-Z].*

If you wanted to test just the first character as being alphabetical and the rest of the string can be anything.

如果您想要测试第一个字符是按字母顺序排列的,其余的字符串可以是任何东西。