Javascript 如何使正则表达式匹配大小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7411194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:11:07  来源:igfitidea点击:

How can I make a regular expression match upper and lower case?

javascriptregex

提问by SoulieBaby

I don't really know much about regex at all, but if someone could help me change the following code to also allow for lowercase a-z, that would be great!

我对正则表达式一无所知,但如果有人能帮我更改以下代码以允许小写 az,那就太好了!

$("input.code").keyup(function(){
    this.value = this.value.match(/[A-Z]{3}([0-9]{1,4})?|[A-Z]{1,3}/)[0];
});

回答by cheeken

If you want a regular expression to be case-insensitive, add a imodifierto the end of the regex. Like so:

如果您希望正则表达式不区分大小写,请在正则表达式的末尾添加i修饰符。像这样:

/[A-Z]{3}([0-9]{1,4})?|[A-Z]{1,3}/i

回答by Benjamin Udink ten Cate

/[A-Za-z]{3}([0-9]{1,4})?|[A-Za-z]{1,3}/

[]denotes a character class and A-Zis a allowed range and means ABCDEFGHIJKLMNOPQRSTUVWXYZ. You can extend this easy by adding a-z

[]表示一个字符类,A-Z是一个允许的范围,表示 ABCDEFGHIJKLMNOPQRSTUVWXYZ。您可以通过添加来轻松扩展a-z