title: Convert a string into a regular expression
body: <p>I am using a very fine JavaScript library called <a href="https://github.com/jacwright/array-query/blob/master/lib/query.js" rel="noreferrer">"array-query"</a> by Jacob Wright to do searches in arrays of objects.</p>
  
  <p>One method is <code>regex()</code> where a regular expression can be included in parentheses like this: <code>regex(/[^\w\s]/)</code>. If I hardcode the expression as I just showed it works fine. If I put the same expression in a variable first it does not work, like this: </p>
  
  <pre><code>var reg = "/[^\w\s]/"; 
  regex(reg);
  </code></pre>
  
  <p>I was told </p>
  
  <blockquote>
    <p>You are putting quotes around your regex, making it a string. Remove the quotes.</p>
  </blockquote>
  
  <p>Thus </p>
  
  <pre><code>var reg = /[^\w\s]/; 
  regex(reg);
  </code></pre>
  
  <p>works fine.</p>
  
  <p>Problem is I need to accept the user input from an textbox as part of the regular expression. For example if the user types in the letter <code>z</code> it needs to get changed to <code>/z/</code>. Even if I type in <code>/z/</code> the <code>textbox.value</code> returned has the same problem as a <code>var reg = "/z/"</code>. If I hardcode <code>var reg = /z/; regex(reg);</code> it works fine.</p>
  
  <p>How to make a input textbox value of <code>"z"</code> into a form that is <code>var reg = z;</code>?
  Many many thanks for any help or ideas, hope this isn't too confusing.</p>