title: client-side caching of jquery-ui-autocomplete: javascript/jquery regex to match a string "index" of an object
body: <p>In my web app , i am using <a href="http://jqueryui.com/demos/autocomplete/" rel="nofollow noreferrer">jquery ui autocomplete</a> which data are stored in the database <b>(Mysql</b>).
  <br>
  In this application, i have the administration official data <b>And</b> i enable the users to enter their own data. Data will be displayed in the autocomplete drop down list .<br>
  The <b>administration data</b> are limited and never change thus are preloaded and cached in memory, however the <b>user data</b> aren't (since user data grows exponentially and there is no point of using a database if all is to be cached in memory).
  <br></p>
  <p>So i am doing multi levels caching in my web app, of which, i am caching autocomplete's <b>user data</b> on the client side (instead of getting them from db on each search).
  <br></p>
  <p><b>The scenario is as follow:</b><br>
  When the app loads, the admin data (that are preloaded in memory) are loaded and cached in a JS object to be used in the autocomplete mechanism.</p>
  <p>Thus when a user searches for a word, it will check the admin data JS object and if it can't find it, it will go to db and find it in user data.</p>
  <p>So <b>for the case of searching user's data in db</b>, What i am doing right now is the following:</p>
  <pre><code>$( &quot;#search&quot; ).autocomplete({
  source:function(request, response) {
  var results = $.ui.autocomplete.filter(data, request.term);
  if (request.term.toUpperCase() in autocomplete_cache) {
      response($.map(autocomplete_cache[request.term.toUpperCase()], function(item) {
       return {value: item.members.name, md5: item.members.ID}
      }))
     
      return;
     }
  </code></pre>
  <p>where <b>autocomplete_cache</b> is a javascript object:</p>
  <pre><code>var autocomplete_cache = {};
  // ...it gets filled everytime a new $.post gets new data
  ...autocomplete_cache[request.term.toUpperCase()] = data;
  </code></pre>
  <p>For the moment, it is caching the <b> exact term searched by a user</b>.
  <br>
  For example, if a user writes <b>&quot;ABCD&quot;</b> and which is not already cached :<br></p>
  <p><b>--&gt;</b> go to the database (MySQL) do :<i><b>&quot;WHERE term Like %ABCD%&quot;</b></i> <br>
  <b>--&gt;</b> autocomplete_cache[&quot;ABCD&quot;] = <i>&quot;data parsed from mysql&quot;</i></p>
  <p><br>So if the same USER now retypes <b>&quot;ABCD&quot;</b> it will get it from the <b>autocomplete_cache Object</b>. <br>However if the USER type <b>&quot;ABC&quot;</b> it won't get it from the cache object because of the line:</p>
  <pre><code>if (request.term.toUpperCase() in autocomplete_cache)
  </code></pre>
  <p>So what i would like to do is to match what the User types in the cache, so in this scenario if the User types <b>&quot;ABC&quot;</b> (which is contained in cache's <b>&quot;ABCD&quot;</b> String) , the autocomplete will build a drop down list containing <b>&quot;ABCD&quot;</b>.</p>
  <p>Is there a way of doing that? the reason for this requirement instead of finding the exact String index in the cache object , it will find a wider range and thus limit probabilities of needing to go to db to find the new matches.</p>