Re: Regular expression to test and limit number of characters



RobG wrote on 28 feb 2008 in comp.lang.javascript:

RobG wrote:
Bart Van der Donck wrote:
Evertjan. wrote:

/^[\dA-Z]{9,12}$/.test(str) && str.replace(/\d/g,'').length<2

Identical, but more terse:

/^\d{9,12}$/.test(str.replace(/[A-Z]/,1))

Where does the (optional or mandatory) letter fit in the first RegExpr?

Ah, OK, I got it - the replace takes care of it first. I'm a bit slower
than usual tonight.

I like Bart's solution.
That's what the joy of programming is about.

[But it does not cover the mandatoriness of an upperalpha! ;-) ]

An alternative not using test():

!str.replace(/[A-Z]/,1).replace(/\d{9,12}/,'')

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
.