title: How do I perform a Javascript match with a pattern that depends on a variable?
body: <p>The current implementation of <a href="http://remysharp.com/2007/12/28/jquery-tag-suggestion/" rel="nofollow noreferrer">Remy Sharp's jQuery tag suggestion plugin</a> only checks for matches at the beginning of a tag. For example, typing "Photoshop" will not return a tag named "Adobe Photoshop".</p>
  
  <p>By default, the search is case-sensitive. I have slightly modified it to trim excess spaces and ignore case:</p>
  
  <pre><code>for (i = 0; i &lt; tagsToSearch.length; i++) {
      if (tagsToSearch[i].toLowerCase().indexOf(jQuery.trim(currentTag.tag.toLowerCase())) === 0) {
          matches.push(tagsToSearch[i]);
      }
  }
  </code></pre>
  
  <p>What I have tried to do is modify this again to be able to return "Adobe Photoshop" when the user types in "Photoshop".  I have tried using <code>match</code>, but I can't seem to get it to work when a variable is present in the pattern:</p>
  
  <pre><code>for (i = 0; i &lt; tagsToSearch.length; i++) {
      var ctag = jQuery.trim(currentTag.tag);
      if (tagsToSearch[i].match("/" + ctag + "/i")) { // this never matches, presumably because of the variable 'ctag'
          matches.push(tagsToSearch[i]);
      }
  }
  </code></pre>
  
  <p>What is the correct syntax to perform a regex search in this manner?</p>