title: Validate Email with regular expression where the number of extensions in the domain part can vary
body: <p>I need a regular expression to match a pattern like this. I have tried using this -</p>
  <pre><code>if(str.match(/^([a-zA-Z-\.]+)@([a-zA-Z]{1,5})\.([a-zA-Z]{1,5})(\.[a-zA-Z]{1,5})?(\.[a-zA-Z]{2,3})?$/)){
    console.log(&quot;the email is valid&quot;);
  }
  </code></pre>
  <p>But this counts <em>user@aaa.bb.cccc</em> as valid whereas it should <strong>not be</strong>.</p>
  <p>The full requirements of the email to be valid -</p>
  <ol>
  <li><p>The username part of the email can have only words(case insensitive), dots(.) and dashes(-). Length
  of 1 or more characters.</p>
  </li>
  <li><p>The domain name part can have 2 to 4 extensions. For example, username@ext.ext.ext.ext this
  email has 4 extensions.</p>
  </li>
  <li><p>All the extensions can only have words(case insensitive), and the length can of 1 to 5 characters
  long except the last extension. The last extension can only be of 2 to 3 characters long. For
  Example,</p>
  <p><em>user@aaa.bb.cccc.dd</em> <strong>is valid</strong></p>
  <p><em>user@aaa.bv.cccc</em> <strong>is not valid</strong></p>
  <p><em>I am having trouble with solving this part. As the extension count can vary I don't know How to check if only the last part of the email has 2
  to 3 characters?</em></p>
  </li>
  </ol>
  <p>I know this type of question might be answered before. I am new to Regular Expressions and I could not find a solution to my problem.</p>