简体   繁体   English

正则表达式,长度为25个字符,以字母开头

[英]Regex with 25 char length, starting with alphabet

My 5 are conditions 我的5个条件

  • Trying to write regex for accepting alphanumeric string. 尝试编写正则表达式以接受字母数字字符串。
  • It cannot start with number. 它不能以数字开头。
  • Cannot have _ hence not using \\w . 不能有_因此不使用\\w
  • Length should be exactly 25 characters. 长度应正好为25字符。
  • First 5 should be alphabets and next 20 should be numeric. 前5个应该是字母,接下来的20个应该是数字。

here is my regex 这是我的正则表达式

var str = "Aasdfds133147852369852147";
var Exp = /^([A-Za-z]+[0-9]){25}$/;  //This fails..ERROR
//var Exp = /^([A-Za-z0-9]){25}$/;  This pass ..SUCCESS, but fails my second condition
if(!str.match(Exp)){
alert("ERROR");
}
else {
alert("SUCCESS");
}

Help me tune my code.. 帮我调整代码。

Is this: 这是:

/^[a-zA-Z][a-zA-Z0-9]{24}$/

what you're looking for? 您在找什么?

regexr link ( with multiline) regexr链路具有多行)

Explanation: 说明:

  • [a-zA-Z] checks if the first char is alphabetical. [a-zA-Z]检查第一个字符是否为字母。
  • [a-zA-Z0-9]{24} checks if the next 24 characters are alphanumeric. [a-zA-Z0-9]{24}检查接下来的24个字符是否为字母数字。

You can use this regex: 您可以使用此正则表达式:

var exp = /^[A-Za-z][A-Za-z0-9]{24}$/;

Breakup: 分手:

^                # string start
[A-Za-z]         # match an alphabetic char
[A-Za-z0-9]{24}  # match 24 of an alphabetic char or a digit
$                # string end

It cannot start with number. 它不能以数字开头。

^(?!\d)

Cannot have _ hence not using \\w. 不能有_,因此不使用\\ w。

^(?!\d)[a-zA-Z0-9]

Length should be exactly 25 characters. 长度应正好为25个字符。

^(?!\d)[a-zA-Z0-9]{25}$

我有一个非常简单的解决方法

 var exp = /^[A-Za-z]{5}[A-Za-z0-9]{20}$/;

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

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