Tutorials Point

  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint

  Home     References     Discussion Forums     About TP  

Javascript RegExp - exec Method

previous next AddThis Social Bookmark Button



The exec method searches string for text that matches regexp. If it finds a match, it returns an array of results; otherwise, it returns null.


RegExpObject.exec( string );

Here is the detail of parameters:

  • string : The string to be searched

Return Value:

Returns the matched text if a match is found, and null if not.


<title>JavaScript RegExp exec Method</title>
<script type="text/javascript">
   var str = "Javascript is an interesting scripting language";
   var re = new RegExp( "script", "g" );

   var result = re.exec(str);
   document.write("Test 1 - returned value : " +  result); 

   re = new RegExp( "pushing", "g" );
   var result = re.exec(str);
   document.write("<br />Test 2 - returned value : " +  result); 

This will produce following result:

Test 1 - returned value : script
Test 2 - returned value : null 

To understand it in better way you can Try it yourself.

previous next Printer Friendly