title: Javascript RegEx causes message loop and crashes browser when set to anything other than /mg
body: <p>I've created a script (my first) that accepts input text and then runs about 30 regular expressions on the text, outputting discovered values into an HTML table. The purpose is for me to be able to paste in text containing specification information for products and have them uniformly outputted so I can paste them into a spreadsheet.</p>
  <p>I've had it working really well and I've been tuning the regexes as I've pasted data with different variations/layouts in. However, I've hit an impasse and need some assistance:</p>
  <p>One of the regular expressions searches for the product part number (sku) and returns the value in a column. Some of the source data includes more than one match because there are regional variations to the products. In all cases the first match is the only one that I want. I've tested the RegEx on RegEx101 and it returns the first match only with the 'global' flag switched off. However, the same RegEx running in my script causes it to return console messages infinitely before crashing. It's immediately unresponsive so I can't see any error messages.</p>
  <p>Here's a sample of the regex section in my script. sku being the one that's causing problems:</p>
  <pre><code>let wallMountable = /(?&lt;wallmountable&gt;[Ww]all [Mm]ount)/mg;
  let sku = /^.*\((?&lt;sku&gt;\d\d\w\w[\d\w]+?).+?\).*$/m;
  
  function parseData() {
  // Execute the Regular Expressions on the text in 'specSheet'
  let specSheet = document.getElementById(&quot;specSheet&quot;).value;
  let wallMountableToken = wallMountable.exec(specSheet);
  let skuToken = sku.exec(specSheet);
  
  do {
  // If Token isn't null, then test to see if the regex group value is undefined. If either are true, do nothing, otherwise write the value to the document.
  if (wallMountableToken !== null) {
    if (wallMountableToken.groups.wallmountable !== undefined)
      {document.write(`${wallMountableToken.groups.wallmountable}`);}
    else {}
    } 
  else {}
  if (skuToken !== null) {
    if (skuToken.groups.sku !== undefined)
      {document.write(`${skuToken.groups.sku}`);}
    else {}
    } 
  else {}
  }
  
  // Loop through the script until a condition is met.
  while (
      (wallMountableToken = wallMountable.exec(specSheet)) !== null,
      (skuToken = sku.exec(specSheet)) !== null
    );
  }
  </code></pre>
  <p>The while loop may not be necessary and in truth I'm not entirely sure what purpose it serves here, but it seemed to consistently appear in the reference material I was studying. I have included it here because it's part of the script, but please note that the script works if I change the second regex to /mg instead of /m, however, it returns multiple values and I only want it to return the first capture.</p>
  <p>I know there's a lot wrong with the script, but this particular question is about why the regex is causing an infinite loop, so any help toward that goal is much appreciated.</p>