We now have a youtube channel. Subscribe!

The Regular Expressions Methods in JavaScript



Hello guys! welcome to my new tutorial on JavaScript. In this tutorial post am going to discussing about the RegExp Methods. I will be starting with the exec method, so make sure to read through very carefully.

The RegExp exec Method

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.

Syntax

Its syntax is as follows -

RegExpObject.exec( string );

Parameter Details

string - The string to be searched

Return Value

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

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript RegExp exec Method</title>
     </head>

     <body>
          <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 );            
          </script>
     </body>
</html>

Output

Below is the output of the above example.

Test 1  -  Returned Value:  script 
Test 2  -  Returned Value:  null 



The RegExp test Method

The test method searches string for text that matches regexp. If it finds a match, it returns true; otherwise, it returns false.

Syntax

Its syntax is as follows -

RegExpObject.test( string );

Parameter Details

string - The string to be searched

Return Value

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

Example

Try the following example below.

<html>
     <head>
          <title> JavaScript RegExp test Method</title>
     </head>

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

               var  result =  re.test( str );
               document.write("Test 1  -  Returned Value:  "  +  result );     

               re  =  new  RegExp( "pushing" , "g" );       

               var  result =  re.test( str );
               document.write("<br />Test 2  -  Returned Value:  "  +  result );            
          </script>
     </body>
</html>

Output

Below is the output of the above example.

Test 1  -  Returned Value:  true 
Test 2  -  Returned Value:  false 


The RegExp toSource Method

The toSource method represents the source code of the object. Take note that this method is not supported by all browsers.

Syntax

Its syntax is as follows -

RegExpObject.toSource(  );

Return Value
Returns the string representing the source code of the object.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript RegExp toSource Method</title>
     </head>

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

               var  result =  re.toSource( str );
               document.write("Test 1  -  Returned Value:  "  +  result );     

               re  =  new  RegExp( "/" , "g" );       

               var  result =  re.toSource( str );
               document.write("<br />Test 2  -  Returned Value:  "  +  result );            
          </script>
     </body>
</html>

Output
Below is the output of the above example.

Test 1  -  Returned Value:  /script/g
Test 2  -  Returned Value:  /\//g

You can also read our tutorial post on: JavaScript Array object with examples 


The RegExp toString Method
The RegExp toString method returns a string representation of a regular expression in the form of a regular expression literals

Syntax
Its syntax is as follows -

RegExpObject.toString(  );

Return Value
Returns the string representing only a regular expression.

Example
Try the following example below.

<html>
     <head>
          <title> JavaScript RegExp toString Method</title>
     </head>

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

               var  result =  re.toString( str );
               document.write("Test 1  -  Returned Value:  "  +  result );     

               re  =  new  RegExp( "/" , "g" );       

               var  result =  re.toString( str );
               document.write("<br />Test 2  -  Returned Value:  "  +  result );            
          </script>
     </body>
</html>

Output
Below is the output of the above example.

Test 1  -  Returned Value:  /script/g
Test 2  -  Returned Value:  /\//g

Alright guys!  we have come to the end of this tutorial on JavaScript RegExp Methods. In my next tutorial post, i will be discussing about the JavaScript Document Object Model.

Feel free to ask your questions via the comment box below. Do follow us on all our social media platforms available. 

Thanks for reading and bye for now. 

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain