Regex Pattern working in regex tester but not in actual code
<p>The following regex check is not working in the code. But if i use this pattern at regex101.com it works perfectly</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var pattern = "^([a-zA-Z0-9]([-\.\w]*[a-zA-Z0-9])*@([a-zA-Z0-9][-\w]*[a-zA-Z0-9]\.)+[a-zA-Z]{2,9})$";
var value = "test@user.com";

var regexp = new RegExp(pattern);
if (!regexp.test(value)) {
   alert("Failed");
} else {
 alert("passed");
}</code></pre>
</div>
</div>
</p>

<p>Could you please help me why this is happening here. By the way if i make some modifications like given below, it works. But i want it to work with (new RegExp(pattern))</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var pattern = /^([a-zA-Z0-9]([-\.\w]*[a-zA-Z0-9])*@([a-zA-Z0-9][-\w]*[a-zA-Z0-9]\.)+[a-zA-Z]{2,9})$/;
var value = "test@user.com";

if (!pattern.test(value)) {
   alert("Failed");
} else {
 alert("passed");
}</code></pre>
</div>
</div>
</p>