title: Js works definitely on the same code with regex
body: <p>I've just run into an interesting case where I don't understand why this code works like that.
  Steps to reproduce:</p>
  <ol>
  <li>Copy mail from the existing div I hardcoded</li>
  <li>Insert into the email field</li>
  <li>Click once or two times on the &quot;Add&quot; button</li>
  </ol>
  <p>Expected:</p>
  <p>New divs should have been added without highlighting the input</p>
  <p>Actual:</p>
  <p>It adds new div but second time it highlights input field</p>
  <p>Why? I tried to log those values but still don't understand <strong>LOOK FULL EXAMPLE ON</strong></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>const el = document.getElementById('emails');
  const input = document.getElementById('input')
  let reg = /^(\w|\-)+@[a-zA-Z0-9][-_.}?+[a-zA-Z]+\.[a-z]{2,4}$/gi
  let prevVal = input.value;
  
  function myFunction() {
    const inputVal = input.value;
    const isInvalid = !reg.test(inputVal);
    console.log('val:', inputVal, isInvalid, prevVal === inputVal)
    if (isInvalid) {
      console.log('Wrong email');
      input.style.borderColor = 'red';
    } else {
      const newDiv = document.createElement('div');
      newDiv.innerText = inputVal;
      newDiv.classList.add('email')
      el.appendChild(newDiv);
    }
    prevVal = inputVal
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>button {
    border: 2px solid black;
    border-radius: 10px;
    padding: 10px 40px;
    cursor: pointer;
  }
  
  .email {
    border: 2px solid black;
    padding: 5px 8px;
    margin: 10px 0;
    cursor: pointer;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Title&lt;/title&gt;
    &lt;link rel="stylesheet" href="stylejs.css"&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;div&gt;
      &lt;input type="text" id="input"&gt;
      &lt;button onclick="myFunction()"&gt;ADD&lt;/button&gt;
      &lt;!-- inputs --&gt;
  
      &lt;div id="emails"&gt;
        &lt;div class="email"&gt;mail@mail.ru&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;script src="indexjs.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>