How to write a JavaScript RegExp to match an expression?


To match an expression, use the JavaScript match() method. This method is used to retrieve the matches when matching a string against a regular expression. The following is the parameter −

  • param − A regular expression object.

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

You can try to run the following code to math an expression using JavaScript Regular Expression −

<html>
   <head>
      <title>JavaScript String match() Method</title>
   </head>
   <body>
      <script>
         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>

Updated on: 23-Jun-2020

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements