title: Regex to match first n characters of a string
body: <p>I am creating a regex to query a MySQL database dynamically based on some text. I have the text parsed, and now I need to make a regex based off of the text that will match a string that contains the first N characters of the text with a minimum of 3 characters. So far I am doing this:</p>
  
  <pre><code>var regex = new RegExp('^' + text.substr(0, 3).replace(/[[\]{}()*+?.\\|^$\-,&amp;#\s]/g, '\\$&amp;'));
  </code></pre>
  
  <p>That works to make sure the first three characters match, but then I want to query the database to see if there are any matches for the rest of the text. For example, if the text was 'test string', I want to make a regex that will match columns in the database with values such as 'tes', 'test str', 'test string', etc. only up to as far as the text goes.</p>
  
  <p>I wasn't really sure how to word the title, so hopefully I did not miss any similiar questions. If there is a better way to query the database for this, I am open to that too.</p>