title: Using regex to limit account IDs input in a field
body: <p>I have a form on a website using Angular 1.6 and one of the fields allows users to input account IDs on which an action should be performed. (There are actually several of these forms all powered by the same controller.)</p>
  
  <p>Users are asked to separate the IDs by any non-alphanumeric character.</p>
  
  <p>On some instances of these forms, however, I need to restrict the number of IDs that can be specified.  (All IDs fit the pattern <code>[a-z0-9]</code>.)  I have gotten close to what I need with the following RegEx pattern and ng-pattern.  The regexp is stored in a var in app.js and the var is referenced in the HTML:</p>
  
  <pre><code>new RegExp(`^(?:(?=([a-z0-9]+))\\1[^a-zA-Z0-9]*){1,${maxSites}}$`);
  </code></pre>
  
  <p>Unfortunately, this also matches commas, whitespace, etc. after an account ID.  For instance, my RegEx matches <code>foobar,</code> ("foobar" followed by a comma and a space) when it shouldn't.</p>
  
  <p>Here is a table showing of what I expect a working RegEx to match based on <code>maxSites</code>:</p>
  
  <pre>
  Input                       | Max | Max 2 | Max 3
  foobar                      | yes |  yes  | yes  
  foobar,                     | no  |  no   | no  
  foobar,                     | no  |  no   | no  
  foobar,bizbaz               | no  |  yes  | yes  
  foobar, bizbaz1             | no  |  yes  | yes  
  foobar bizbaz1              | no  |  yes  | yes  
  foobar, bizbaz1,            | no  |  no   | no  
  foobar, bizbaz1, binbong2   | no  |  no   | yes  
  </pre>
  
  <p>Is there a way to do this with RegExp?</p>