title: Dynamically create RegExps groups to match patterns lower than or greater than a desired value
body: <p>I've made two <code>JavaScript</code> functions to dynamically create <code>RegExps</code> groups that match numbers lower than or greater than the number that is sent in the parameter. The purpose of these functions is to do something like <a href="https://stackoverflow.com/questions/11011674/how-to-match-a-number-which-is-less-than-or-equal-to-100">this</a> or <a href="https://stackoverflow.com/questions/11387072/how-can-i-find-values-greater-than-100-using-regular-expression">this</a> but dynamically, I need it for an App for building <code>RegExps</code>, this code made a group that match a particular group of numbers, later you could use the returned group to complete your final <code>RegExp</code>.</p>
  
  <p>Here is the function to create a <code>RegExp</code> to find patterns greater than a desired value:</p>
  
  <pre><code>//Find greater than numbers
  function getGreaterUintRegEx(n) {
    var s = String(n);
    var t = s.length,
      a = [];
    for (var i = 1; i &lt; t + 1; i++) {
      switch (s.charAt(t - i)) {
        case "9":
          a.push((Number(s.slice(0, t - i)) + 1) + "0" + (new Array(i)).join("\\d"));
          break;
        case "8":
          a.push(s.slice(0, t - i) + "9" + (new Array(i)).join("\\d"));
          break;
        default:
          a.push(s.slice(0, t - i) + "[" + (Number(s.charAt(t - i)) + 1) + "-9]" + (new Array(i)).join("\\d"));
      }
    }
    a.push("\\d{" + (t + 1) + ",}");
    a = a.filter(function(s, i) {
      return a.indexOf(s) == i;
    });
    return "(" + a.join("|") + ")";
  }
  </code></pre>
  
  <p>Example of use:</p>
  
  <pre><code>var regstr = getGreaterUintRegEx(124);
  // (12[5-9]|1[3-9]\d|[2-9]\d\d|\d{4,})
  
  var regstr = getGreaterUintRegEx(500);
  // (50[1-9]|5[1-9]\d|[6-9]\d\d|\d{4,})
  </code></pre>
  
  <p>And here is the function to create a <code>RegExp</code> to find patterns lower than a desired value:</p>
  
  <pre><code>//Find lower than numbers
  function getLowerUintRegEx(n) {
    if (n == 0) return false;
    if (n == 1) return "(0)";
    if (n &gt; 0 &amp;&amp; n &lt; 10) return "[0-" + (n - 1) + "]";
    var s = String(n);
    var t = s.length,
      a = [];
    for (var i = 1; i &lt; t + 1; i++) {
      switch (s.charAt(t - i)) {
        case "0":
          a.push(((s.slice(0, t - i) == "1") ? "" : (Number(s.slice(0, t - i)) - 1)) + "9" + (new Array(i)).join("\\d"));
          break;
        case "1":
          a.push("[1-9]" + (new Array(i - 1)).join("\\d"));
          break;
        default:
          a.push(s.slice(0, t - i) + "[0-" + (Number(s.charAt(t - i)) - 1) + "]" + (new Array(i)).join("\\d"));
      }
    }
    if (t - 1 &gt; 1) a.push("\\d{1," + (t - 1) + "}");
    a.push("0");
    a = a.filter(function(s, i) {
      return a.indexOf(s) == i;
    });
    return "(" + a.join("|") + ")";
  }
  </code></pre>
  
  <p>Example of use:</p>
  
  <pre><code>var regstr = getLowerUintRegEx(498);
  // (49[0-7]|4[0-8]\d|[0-3]\d\d|\d{1,2}|0)
  
  var regstr = getLowerUintRegEx(125);
  // (12[0-4]|1[0-1]\d|[1-9]\d|\d{1,2}|0)
  </code></pre>
  
  <p>I want to make these functions more simply and less slow. It takes more than a second with big numbers. Is there any other easier method? Somebody knows a robust algorithm with less steps?</p>