0

Two snippets are shown below. The first one is pure Js and runs correctly with node.js The second one is just a copy of it encapsulated into some Html page as the function getDate. Surprisingly the html code gives the error SyntaxError: invalid regexp group at line 1. Why ?

    const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
    const result = re.exec('2015-01-02')
    console.log(result);
    console.log('year', result.groups.year);
    console.log('month',result.groups.month);
    console.log('day',result.groups.day);
    <!DOCTYPE html>
    <html>
        <meta charset="utf8"/>
        <body>
     <button onclick="getDate()">Get Date</button>
     <script>
      function getDate() {
          const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
          const result = re.exec('2015-01-02')
          console.log(result);
          console.log('year', result.groups.year);
          console.log('month',result.groups.month);
          console.log('day',result.groups.day);
      }
     </script>

        </body>
    </html>
Emile Achadde
  • 1,715
  • 3
  • 10
  • 22
  • 2
    Your code snippet seems to run fine. What browser is giving you the problem? They are not all created equal. – Booboo Apr 20 '20 at 14:27
  • @ Booboo I use Firefox 68.7.0esr (64 bits) on Debian. It runs fine on Brave ! – Emile Achadde Apr 20 '20 at 14:47
  • 1
    Nor does it work in Edge (but it does work in Chrome). This is why when I code JavaScript to be used in a browser, I code to the lowest common denominator, which is pretty low. Before you use a feature, check [browser compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Browser_compatibility). – Booboo Apr 20 '20 at 15:46

0 Answers0