30

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]; }); 
2
  • 8
    You mean like with /i? Commented Sep 14, 2011 at 4:20
  • What are you trying to match? Your regex looks wrong. Commented Sep 14, 2011 at 4:33

2 Answers 2

58

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 
Sign up to request clarification or add additional context in comments.

Comments

13
/[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

3 Comments

@muntoo probably clashed with my own edit done at the same time adding the other 3 lines.
-1 For suggesting that [a-zA-Z] is equivalent to \w. And why are you using [\w]{3} instead of just \w{3}?
\w allows more than [a-zA-Z] - as Regex Cheat Sheet, says it matches in 'Most engines: "word character": ASCII letter, digit or underscore'. Other references are also available....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.