SoulieBaby
SoulieBaby

Reputation: 5471

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

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!

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

Upvotes: 30

Views: 125817

Answers (2)

Ben
Ben

Reputation: 13635

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

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

Upvotes: 13

cheeken
cheeken

Reputation: 34655

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

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

Upvotes: 58

Related Questions