With JavaScript RegExp how to search a string for text that matches regexp?

HTMLJavascriptProgramming Scripts

To search a string for a text that matches RegExp, use rhe exec() method in JavaScript. If it finds a match, it returns an array of results; otherwise, it returns null.

The following is the parameter −

  • string − The string to be searched


You can try to run the following code to search a string for text matching RegExp −

      <title>JavaScript RegExp exec Method</title>
         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);
Published on 26-Feb-2018 11:29:35