A summary of the usage of match functions in JavaScript

Source: Internet
Author: User
Tags regular expression

  This article is mainly about the use of the match function in JavaScript in a detailed summary of the introduction, the need for friends can come to the reference, I hope to help you.

The match function in JavaScript uses a regular expression to find a string and returns the result of the lookup as an array, which is useful in practical development, using the following:  Stringobj.match (rgexp)   Where stringobj is a required option. A string object or string literal to find it.   RGEXP is a must option. Is the regular expression object that contains the regular expression pattern and the available flags. It can also be a variable name or string literal that contains the regular expression pattern and the available flags.   If the match function method in JavaScript does not find a match, return null. If a match is found to return an array and the properties of the global RegExp object are updated to reflect the result of the match. The match function method in JavaScript returns an array with three properties: input, index, and lastindex. The Input property contains the entire lookup string. The Index property contains the position of the substring that matches the entire lookup string. The Lastindex property contains the next position of the last character in the last match. If the global flag (g) is not set, the 0 element of the array contains the entire match, and the 1th to n element contains the any match that occurred in the match. This is equivalent to an Exec method that does not have a global flag set. If the global flag is set, element 0 to n contains all matches.     The following example demonstrates the use of the Match function method in JS:  function Matchdemo () {  var r, re;//declare variable.   var s = "The rain in Spain falls mainly in the plain";  re =/ain/i; Creates a regular expression pattern.   R = S.match (re); Try to match the search string.   return (R); Return to the place where "Ain" first appeared.  }    This example shows the use of the Match function method in JS with G flags   function Matchdemo () {  var r, re;//declare variable.   var s = "The rain in Spain falls mainly in the plain";  re =/ain/ig; Creating regular Expressions-mode.   R = S.match (re); Try to match the search string.   return (R); The returned array contains four matches for all "ain"  //occurrences.  }     The following lines of code demonstrate the use of the Match function method in JS of string literals.   var r, re = "Spain";  r = "The Rain in Spain". Replace (Re, "Canada"); The     Match () method is used to find a specified value from a string, similar to indexof () and LastIndexOf (), which returns the specified value instead of specifying the position of the value in the string. IndexOf () and LastIndexOf () methods return the position number if no return-1 is found. Note Case sensitive   <script type= "Text/javascript" >  var str= "Hello world!"   document.write (Str.match ("World") + ""]   document.write (Str.match ("World") + "")   document.write ( Str.match ("Worlld") + "")   document.write (Str.match ("world!"))   </script>  
Related Article

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.