Incorrect results when validating email addresses with JavaScript : <p>I have the following regex pattern that I like to use to validate email addresses:</p>

<pre><code>^[a-zA-Z0-9!#\$%&amp;'\*\+\-\/=\?\^_`\{\|\}~\.]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-.]+[a-zA-Z0-9]$
</code></pre>

<p>It's intended for user input and to be pretty forgiving, in that it will allow some fringe cases that aren't valid but will catch most mistakes.</p>

<p>I have been using this for ages in C# applications without issue. Below is a simple C# script that shows it working quite happily:</p>

<pre><code>string pattern = @"^[a-zA-Z0-9!#\$%&amp;'\*\+\-\/=\?\^_`\{\|\}~\.]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-.]+[a-zA-Z0-9]$";

string[] data = new string[] {
    "foo@bar.com",
    "foo@bar.co.uk",
    "foo-foo@bar.com",
    "foo.foo@bar.co.uk",
    "foo_foo_69@bar_123.museum",
    "o'foo@bar.com",
    "foofoo@barbar",
    "Foo@foo@bar.com",
    "@bar.com",
    "foo.com",
    "foo@bar",
    "foo@bar.co."
};

foreach (string email in data)
{
    bool valid = Regex.IsMatch(email, pattern);
    string state = valid ? "pass" : "fail";
    Console.WriteLine($"{state}: {email}");
}
</code></pre>

<p>This little script gives the following output, and is exactly as I'd expect:</p>

<pre><code>pass: foo@bar.com
pass: foo@bar.co.uk
pass: foo-foo@bar.com
pass: foo.foo@bar.co.uk
pass: foo_foo_69@bar_123.museum
pass: o'foo@bar.com
fail: foofoo@barbar
fail: Foo@foo@bar.com
fail: @bar.com
fail: foo.com
fail: foo@bar
fail: foo@bar.co.
</code></pre>

<p>I now need to validate email addresses on the client side in JavaScript. When I try the same thing in JS I get slightly different results! Here is my JS version of the program above:</p>

<pre><code>var pattern = "^[a-zA-Z0-9!#\$%&amp;'\*\+\-\/=\?\^_`\{\|\}~\.]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-.]+[a-zA-Z0-9]$";

var data = [
    /* the same list of emails as above */
];


for (var i = 0; i &lt; data.length; i++) {
    var valid = new RegExp(pattern).test(data[i]);
    var state = valid ? "pass" : "fail";
    console.log(state + ": " + data[i]);
}
</code></pre>

<p>This time I get the following output:</p>

<pre><code>pass: foo@bar.com
pass: foo@bar.co.uk
pass: foo-foo@bar.com
pass: foo.foo@bar.co.uk
pass: foo_foo_69@bar_123.museum
pass: o'foo@bar.com
pass: foofoo@barbar             &lt;- this should fail
pass: Foo@foo@bar.com           &lt;- and this!
fail: @bar.com
fail: foo.com
fail: foo@bar
fail: foo@bar.co.
</code></pre>

<p>Note the two passes in the middle that were failing in the C# version!!! Just to confirm that my regex pattern is OK I used regex101.com's excellent unit test feature to check it out, click <a href="https://regex101.com/r/7hdIvZ/2/tests" rel="nofollow noreferrer">here</a> to see my tests/results. Please note that I have deliberately set it up incorrectly (all tests 'expected' to match) so it highlights invalid email addresses. Any way here is a pic of the results, which as you can see match those I got using C#.</p>

<p><a href="https://i.stack.imgur.com/P32Qf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P32Qf.png" alt="test results"></a></p>

<p>If anyone could point out where I am going wrong I would appreciate it!</p>
