JSFiddle - React, Tailwind, and code Playground

HTML

<!DOCTYPE html>
<html>
<body>

<h1>Matchall API test : Finding all sub strings matching regular expression</h1>
<p> string : "hi this is test1test2"  </p>
<p> regular expression : test\d </p>

<br>
<p> Expected Result : </p> <p>test1 </p> <p>test2 </p>
<br>
<p> Actual Result :</p>
<p id="demo"></p>
<p id="demo2"></p>

<script>
const regexp = /test\d?/g;
const str = 'test1test2';
const array = [...str.matchAll(regexp)];
document.getElementById("demo").innerHTML = array[0];
document.getElementById("demo2").innerHTML = array[1];

</script>

</body>
</html>