PDA

View Full Version : Reg. Exp.


A1ien51
12-30-2002, 06:49 PM
okay I have the following expression

if(XX.value.match(/\A1\b/g))

that works fine and dandy, but I need to change A1 to a variable.

I know the following line is not right, but it shows you what I need done

" /\ " + STRING + "\b/g"



TY
A1ien51

jkd
12-30-2002, 07:05 PM
RegExp(myVar + '\b', 'g')

can take the place of your regexp literal.

beetle
12-30-2002, 07:54 PM
jkd, correct me if I'm wrong, but isn't RegExp a constructor and need the new operator?

jkd
12-30-2002, 10:07 PM
Well, I'll have to correct you then. :D

Yes, RegExp typically functions as a constructor, but it doubles as a sort of typecaster which returns a literal value. The new operator creates an object of type RegExp.

Do you ever see people use
var myNum = new Number(someuserinput);
? Typically not.

An extreme example:

if (Boolean(false)) {
// this never executes
}

if (new Boolean(false)) {
// but this does
}