简体   繁体   English

字符串中冒号的不区分大小写的正则表达式

[英]case-insensitive regex for colon in a string

I am trying to match a string that has the form: 我正在尝试匹配具有以下形式的字符串:

abcd:vxyz

That is: 4 chars followed by a colon then followed by three (or maximum) 4 chars. 即:4个字符,后跟一个冒号,然后是三个(或最多)4个字符。

I want to do case INSENSITIVE matches. 我想进行大小写不匹配的比赛。

Can anyone help with the pattern? 有人可以帮忙吗?

/^[a-z]{4}:[a-z]{3,4}$/i

........

The following regex should work: 以下正则表达式应该起作用:

"^[a-zA-Z]{4}:[a-zA-Z]{3,4}$"

The {4} part indicates that it should match exactly four copies of the previous symbol, which can be any character between 'a' and 'z', as well as 'A' and 'Z', inclusive. {4}部分表明它应与前一个符号的四个副本完全匹配,该符号可以是“ a”和“ z”之间的任何字符,也可以是“ A”和“ Z”之间的任何字符。

The {3,4} part creates a range of copies between 3 and 4 inclusive, while the '^' symbol indicates that it should start at the beginning of the given string and the '$' sign indicates that it should end at the end of the given string. {3,4}部分创建了3到4之间的副本范围(包括3和4),而'^'符号表示它应该从给定字符串的开头开始,而'$'符号表示它应该在结尾处结束给定字符串的。

Your example was alphabetical, but it was unclear if your desired regex should be limited to that. 您的示例是按字母顺序排列的,但尚不清楚您所需的正则表达式是否应限于此。 If you wanted to match any characters in those groups: 如果要匹配这些组中的任何字符:

/^.{4}:.{3,4}$/i

Another non-regex way to do this would be to split() on the colon. 另一种非正则表达式的方法是在冒号上使用split() Check for length 4 on the first element, and length 3 or 4 on the second element. 检查第一个元素的长度为4,第二个元素的长度为3或4。

var foo = 'abcd:123a';
var bar = 'fds:0';

var af = foo.split(':');
var isMatch = ((af[0].length==4) && (af[1].length==3 || af[1].length==4));
alert (isMatch);

var ab = bar.split(':');
alert ((ab[0].length==4) && (ab[1].length==3 || ab[1].length==4));

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

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