Usage of match function in JavaScript _javascript tips

Source: Internet
Author: User

The match function in JavaScript uses a regular expression to find the string and returns the result of the lookup as an array, which is useful in practical development, using the following methods:
Stringobj.match (RGEXP)
Where stringobj is a required option. A string object or string literal to find it.
Rgexp is a required 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.

Returns null if the Match function method in JavaScript does not find a matching. 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 shows the use of the Match function method in JS:
function Matchdemo () {
var r, re; Declare a 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 with the G flag setting in JS
function Matchdemo () {
var r, re; Declare a variable.
var s = "The rain in Spain falls mainly in the plain";
re =/ain/ig; Creates a regular expression pattern.
r = S.match (re); Try to match the search string.
return (R); The returned array contains all the "Ain"
Four matches that occurred.
}

The following lines of code illustrate 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 that case sensitivity
<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.