# RegExp based on variable?

**URL:** <https://forum.kirupa.com/t/regexp-based-on-variable/272065>\
**Category:** flash\
**Created:** [October 1, 2008, 7:19pm UTC](https://forum.kirupa.com/t/regexp-based-on-variable/272065 "2008-10-01T19:19:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stickeroo](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@Stickeroo](https://forum.kirupa.com/u/Stickeroo)\
**Post date:** [October 1, 2008, 7:19pm UTC](https://forum.kirupa.com/t/regexp-based-on-variable/272065/1 "2008-10-01T19:19:19Z")

</div>

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:

```auto

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 ☹ 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):

```auto

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) ☹

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

```auto

/\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:

```auto

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

```

does the trick nicely! Carry on, carry on… 🙂

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