RegExp based on variable?

I have an array of strings (that look like numbers) that ultimately gets joined to look, say, like this:

0|1|5|8|9|12

I’m trying to avoid having to iterate the array [0,1,5,8,9,12] by just searching through a string. The problem I’m having is this: the following:


var testString:String="12";
var arrDigits:Array=new Array("0", "1", "5", "8", "9", "12");
if (arrDigits.join("|").match(testString)) { // Matches 1, 2, and 12 :(
    // Do stuff
}

Obviously, not what I want :frowning: I know I can use a regexp to handle it, but I’m not sure how to put testString into my pattern. This (predictably) would work for all numbers (which isn’t what I want):


match(/\b[\d]+\b/)

Basically, I’d like to be able to check testString (which might not always be the same thing) against word boundaries, which I’m not sure how to do without regexp (or with regexp, apparently) :frowning:

If anyone could share some insight into how to effectively create a functional regexp based on this:


/\b/ + testString + /\b/

I would love to know how!!! Thanks!

(Also, if there’s a way to accomplish what I’m trying to do without regexp [or traversing an array], I’m interested in that, too…I think I’ve got my regexp hat on and can’t take it off…)

EDIT:

All right, I figured it out:


match(new RegExp('\\b'+testString+'\\b'));

does the trick nicely! Carry on, carry on… :slight_smile:

Edit 2: Krilnon, thanks! You were probably posting just as I edited my post! :slight_smile: