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

What is the role of exec method in JavaScript RegExp?


The exec() method in JavaScript searches for a string with a pattern. Returns NULL, if no match is found, else returns the text you searched for.

Example

You can try to run the following code to learn how to work with exec() method in JavaScript Regular Expression −

<html>
   <head>
      <title>JavaScript Regular Expressions</title>
   </head>
   <body>
      <script>
         var myStr = "Welcome to our website! Welcome to Tutorialspoint!";
         var res = /t/.exec(myStr);
         document.write(res);
      </script>
   </body>
</html>