简体   繁体   English

正则表达式不匹配字符串

[英]Regular expression not matching string

I'm trying to figure out the following regular expression: 我想弄清楚以下正则表达式:

/^[0-9]{2}-[0-9]{2,3}[a-zA-z]{0,1}/g

In my example. 在我的例子中。
The following should pass: 00-45, 00-333, 33-333a, 55-34a The following should fail: 33-3333, 22-22dd, 22-2233 以下内容应通过:00-45,00-333,33-333a,55-34a以下内容应失败:33-3333,22-22dd,22-2233

Here is my screenshot: 这是我的截图:

在此输入图像描述

But the once that should fail arent failing. 但那应该失败的一次也不会失败。 In my javascript code I just do a test: 在我的javascript代码中,我只是做一个测试:

var regExp = new RegExp(exp);
if(regExp.test(test1))
    alert('pass');
else
    alert('fail');

Is there a way for the regular expression to test the entire string? 有没有办法让正则表达式测试整个字符串? Example 33-3333 passes because of 33-333, but since there is another 3 I would like it to fail since the fourth 3 would be tested against the character rule? 例33-3333因为33-333而通过,但由于还有另外3个,我希望它失败,因为第四个3将根据字符规则进行测试?

  1. You are missing end anchor $ in your input 您在输入中缺少结束锚$
  2. Az inside character class will match unwanted characters as well, you actually need AZ 字符类中的Az也会匹配不需要的字符,你实际上需要AZ
  3. {0,1} can be shortened to ? {0,1}可以缩短为?

Try this regex: 试试这个正则表达式:

/^[0-9]{2}-[0-9]{2,3}[a-zA-Z]?$/

RegEx Demo RegEx演示

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

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