title: Regex to use in JavaScript string with not more than n number of digits
body: <p>I'm trying to validate a username in jquery using a regex, with following rules</p>
  
  <ul>
  <li><p>String must start with a letter</p></li>
  <li><p>String can have 0(m) to 4(n) digits anywhere or no more than 4(n)</p></li>
  <li><p>Size of the string must be between 6(p) and 20(q)</p></li>
  </ul>
  
  <p><strong>Following should fail</strong></p>
  
  <ul>
  <li><p><code>1djgWWq</code> (starts with a digit)</p></li>
  <li><p><code>wwwhg</code> (length less than 6)</p></li>
  <li><p><code>ky12yu781</code> (more than 4 digits)</p></li>
  </ul>
  
  <p><strong>Following should pass</strong></p>
  
  <ul>
  <li><p><code>tableten</code></p></li>
  <li><p><code>table10</code></p></li>
  <li><p><code>tab7le10</code></p></li>
  </ul>
  
  <p>I tried following regex which is good, except its not match 4 digits anywhere in the string</p>
  
  <pre><code>^(?=^[A-Za-z]+[A-Za-z0-9_-]{5,20}$)(?=^(?:(?!\d{4}).)*$).*$
  </code></pre>
  
  <p><a href="https://regex101.com/r/eB8fF9/1" rel="nofollow">Sample regex101</a></p>
  
  <p>Note: m,n, p,q integer variable</p>
  
  <blockquote>
    <p>EDIT 1</p>
  </blockquote>
  
  <p>Sebastian Proske> Yes letters numbers and _ -
  I want to try validate fields in a form using different regex for each field, so regex per one filed, I want to stick to one regex for cleaner code.
  bellow is sample untested code to give you a idea what I'm trying to do.
  Sebastian Proske answer is the best one for my work. Thanks everyone.</p>
  
  <pre><code>Sample 1&lt;input id="ds1" type="text" class="validate" data-validate='{"regex":"^[a-z0-9]{4,7}$"}' /&gt;&lt;br/&gt;
  Sample 2...
  Sample 3...
  Active Sample &lt;input id="as" type="text" class="validate" data-validate='{"regex":"^(?=^[A-Za-z]+[A-Za-z0-9_-]{5,20}$)(?=^(?:(?!\d{4}).)*$).*$"}' /&gt;&lt;br/&gt;
  &lt;input id="f1SubmitBtn" type="button" value="Test" onclick="f1Submit();" /&gt;&lt;br/&gt;
  &lt;script&gt;
    function f1Submit() {
    $('.validate').each(function() {
      if (!$(this).data('validate').regex.test(this.value)) {
        console.log(this.value + " No!");
      }
    });  
  &lt;/script&gt;
  </code></pre>