The JavaScript Match () method uses the

Source: Internet
Author: User
Tags array length

1. JavaScript Match () method description
Http://www.w3school.com.cn/jsref/jsref_match.asp

Writing:

Stringobject.match (Searchvalue) stringobject.match (regexp)

Return value: The array that holds the matching result.

2. Match method Regular expression parameter usage

2.1 Regular Expression no global flag g

If RegExp does not have a flag G, then the match () method can only perform a match in Stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array that holds information about the matching text it finds. The No. 0 element of the array holds the matching text, while the rest of the elements hold the text that matches the subexpression of the regular expression. In addition to these regular array elements, the returned array contains two object properties. The Index property declares the position of the starting character of the matching text in Stringobject, and the input property declares a reference to Stringobject.

Example code:

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "Utf-8" />        <title>The JavaScript Match () method uses the</title>        <style>            * {margin:0;padding:0;font-size:14px;font-family:"Microsoft Yahei";box-sizing:Border-box;            }P{text-indent:2em;Line-height:25px;            }        </style>    </Head>    <Body>        <P>front-end for a website, usually refers to the site's foreground part includes the site's performance layer and structure layer. So front-end technology is generally divided into front-end design and front-end development, the front-end design can generally be understood as the visual design of the site, front-end development is the site's foreground code implementation, including basic HTML and CSS and Javascript/ajax, now the latest advanced version of HTML5, CSS3,        , as well as SVG. </P>        <Scriptsrc= "Https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></Script>        <Scripttype= "Text/javascript">            $(function() {                vararr= NewArray (), str= $('P'). text (); //Regular Expression No global flag Garr=Str.match (/(front end)/); //returns an array of resultsConsole.log (arr); //returns the result array length The result of this example is: 2Console.log ('returns the result array length:' +arr.length); //returns the first element of the result array the result of this example is: Front endConsole.log ('returns the first element of a result array:' +arr[0]); //returns the second element of the result array the result of this example is: Front endConsole.log ('returns the second element of a result array:' +arr[1]); })        </Script>    </Body></HTML>

The result is:

Note: If the regular expression has no global flag G,match does not match, the returned result array is null; if matched, the array returns 2 elements, the first element matches the text of the regular expression, and the second matches the text .

2.2 Regular expressions have global flag g

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= "Utf-8" />        <title>The JavaScript Match () method uses the</title>        <style>            * {margin:0;padding:0;font-size:14px;font-family:"Microsoft Yahei";box-sizing:Border-box;            }P{text-indent:2em;Line-height:25px;            }        </style>    </Head>    <Body>        <P>front-end for a website, usually refers to the site's foreground part includes the site's performance layer and structure layer. So front-end technology is generally divided into front-end design and front-end development, the front-end design can generally be understood as the visual design of the site, front-end development is the site's foreground code implementation, including basic HTML and CSS and Javascript/ajax, now the latest advanced version of HTML5, CSS3,        , as well as SVG. </P>        <Scriptsrc= "Https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></Script>        <Scripttype= "Text/javascript">            $(function() {                vararr= NewArray (), str= $('P'). text (); //Regular Expression No global flag Garr=Str.match (/(front end)/g); //returns an array of resultsConsole.log (arr); //returns the result array length The result of this example is: 6Console.log ('returns the result array length:' +arr.length); })        </Script>    </Body></HTML>

Results:

If RegExp has the flag G, the match () method performs a global retrieval and finds all matching substrings in the stringobject.

Summary:regular Expressions for the Macth () method do not forget to use the global flag G.

The JavaScript Match () method uses the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.