Home > Web Front-end > JS Tutorial > How to use match method

How to use match method

不言
Release: 2020-09-12 14:02:06
Original
43711 people have browsed it

Usage of the match method: Retrieve the results matching the regular expression in the string and return the matching items as Array objects. The syntax of the match method is "string.match(param)", where the parameter "param ” represents a regular expression object. The

How to use match method

match method is used to retrieve the results that match the regular expression in the string and return the matching items as an Array object. So in this article we will talk about the basic usage of the match method.

Let’s first take a look at the basic syntax of the match method

string.match( param )
Copy after login

param represents a regular expression object.

Note: If the regular expression does not contain the g modifier (performing a global search), the match() method will only return the first match in the string.

If no match is found, this method returns null.

Let’s look at a specific example

The code is as follows

<!DOCTYPE html>
<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>
Copy after login

The display effect on the browser is: Chapter 3.4.5.1,Chapter 3.4.5.1,.1

This article ends here. For more exciting content, you can pay attention to the relevant column tutorials on the php Chinese website! ! !

The above is the detailed content of How to use match method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template