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>