JavaScript String - match() Method


Advertisements


Description

This method is used to retrieve the matches when matching a string against a regular expression.

Syntax

Use the following syntax to use the match() method.

string.match( param )

Argument Details

param − A regular expression object.

Return Value

  • If the regular expression does not include the g flag, it returns the same result as regexp.exec(string).

  • If the regular expression includes the g flag, the method returns an Array containing all the matches.

Example

Try the following example.

<html>
   <head>
      <title>JavaScript String match() Method</title>
   </head>
   
   <body>
   
      <script type="text/javascript">
         var str = "For more information, see Chapter 3.4.5.1";
         var re = /(chapter \d+(\.\d)*)/i;
         var found = str.match( re );
         
         document.write(found ); 
      </script>
      
   </body>
</html>

Output

Chapter 3.4.5.1,Chapter 3.4.5.1,.1

javascript_strings_object.htm