Summary of the use of the match function in javascript

  • 2020-03-30 01:38:11
  • OfStack

The match function in javascript USES the regular expression to find the string and returns the result of the search as an array, which is very useful in the actual development. The method is as follows:
StringObj. Match (rgExp)
Where stringObj is the mandatory option. The String object or String literal to which it is looked up.
RgExp is required. Is a regular expression object that contains regular expression patterns and available flags. It can also be a variable name or string literal that contains the regular expression pattern and available flags.

If the match function method in javascript does not find a match, return null. Returns an array if a match is found and updates the properties of the global RegExp object to reflect the match. The array returned by the match function method in JavaScript has three properties: input, index, and lastIndex. The Input property contains the entire searched string. The Index property contains the position of the matching substring across the searched string. The LastIndex attribute 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 1 through n elements contain any child match that ever occurred in the match. This is equivalent to an exec method with no global flag set. If the global flag is set, elements 0 through n contain all matches.

The following example demonstrates the use of the match function method in js:
The function MatchDemo () {
Var r, re; // declare variables.
Var s = "The rain in Spain falls is in The plain";
Re = / ain/I; // create a regular expression pattern.
R = s.m atch (re); // attempts to match the search string.
Return (r); // return to the place where "ain" first appeared.
}

This example illustrates the use of the match function method in js with a g flag setting
The function MatchDemo () {
Var r, re; // declare variables.
Var s = "The rain in Spain falls is in The plain";
Re = / ain/ig; // create a regular expression pattern.
R = s.m atch (re); // attempts to match the search string.
Return (r); // the array returned contains all "ain"
// four matches appear.
}  

The following lines of code illustrate the use of the match function method in js for string literals.
Var r, re = "Spain";
Replace (re, "Canada");  

The match() method is used to find the specified value from a string, similar to indexOf() and lastindexOf(), except that it returns the specified value instead of its position in the string. The indexOf() and lastindexOf() methods return the position number if it cannot find -1. Be case sensitive
< The script type = "text/javascript" >
Var STR = "Hello world!"
Document. The write (STR) match (" world ") + "")
Document. The write (STR) match (" World ") + "")
Document. The write (STR) match (" worlld ") + "")
Document. The write (STR. Match (" world!" ))
< / script>


Related articles: