Question:

How do you dereference a variable for use in a JavaScript regex?

by  |  earlier

0 LIKES UnLike

I have an array element that I wish to see if it matches the contents of an array element from a different array in JavaScript. I wish to create a pattern variable like \element 1\, where element 1 is from the first array. I could then say something like pattern.test(element 2), where element 2 is from array 2. Element 1, however, is a variable, and I want to use its contents when building the pattern variable. Can you help?

 Tags:

   Report

1 ANSWERS


  1. try using a loop:

    var array1 = array("blue","brown","green");

    var array2 = array("black","white","green");

    for(i=0,i<array1.count,i++){

        for(e=0,e<array2.count,e++){

               if(array1[i] == array2[e]){

                       do this

              }

              else{

                     do this

               }

        }

    }

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.