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

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 23-Feb-2018 11:34:09
Advertisements