Computer >> Computer tutorials >  >> Programming >> Javascript

How do you access the matched groups in a JavaScript regular expression?


To access the matched groups in a JavaScript regular expression, you can try to run the following code −

Example

<html>
   <head>
      <script>
         var str = "Username akdg_amit";
         var myReg = /(?:^|\s)akdg_(.*?)(?:\s|$)/g;

         var res = myReg.exec(str);

         document.write(res[1]);
      </script>
   </head>
   <body>
   </body>
</html>