views:

92

answers:

2

I am getting myArray as null. Can anyone help me?

myRe = new RegExp ("[A-Z]+(\\d+)");
myArray = myRe.exec("book1");
alert(myArray.length);
+6  A: 

Your regular expression is case sensitive; try:

myRe = new RegExp ("[A-Za-z]+(\\d+)");

or:

myRe = new RegExp ("[A-Z]+(\\d+)", "i");
RichieHindle
Or add an i parameter: new RegExp('foo', 'i');
Pim Jager
My fault I was using RegexBuddy with Case Insensitive mode turned on. Thank you for the quick answer.
Sergio del Amo
+2  A: 

It's because you use [A-Z] which is for uppercase.

Use this instead:

pattern = /[a-z](\d+)/i;
myArray = pattern.exec("BOOK1");
alert(myArray.length);
Boris Guéry