The MATCH function in JavaScript is to find the string with regular expressions and return the finding results as an array. It is very useful in actual development. The method of use is as follows:
stringObj.match (RGEXP)
StringObj is a must -have. String object or string text that searches for it.
RGEXP is a must -have. To include regular expression patterns and regular expression objects available. It can also include a regular expression mode and a variable name or string text that available signs.
If the MATCH function method in JavaScript does not find matching, return NULL. If you find a matching return and update the attributes of the global regexp object to reflect the matching results. There are three attributes returned by the Match function method in JavaScript: Input, Index, and LastIndex. The INPUT property contains the entire search string. The index property contains the position of the sub -string that is matched in the search string throughout. The LastIndex property contains the next position of the last character in the last match. If the global logo (G) is not set, the 0 element of the array contains the entire match, and the 1st to N element contains any sub -match that has appeared in the matching. This is equivalent to the EXEC method without a global logo. If the global logo is set, the element 0 to n contains all the matching.
The following example demonstrates the usage method of the MATCH function method in JS:
function matchdemo () {
VAR R, Re; // Declars variables.
var s = "the rain in spain falls mainly in the play";
Re = /Ain /i; // Create a regular expression mode.
r = s.match (re); // Try to match the search string.
Return (R); // Back to the place where "Ain" appeared for the first time.
}
This example describes the usage method method of the MATCH function method set with G flag.
function matchdemo () {
VAR R, Re; // Declars variables.
var s = "the rain in spain falls mainly in the play";
Re = /Ain /Ig; // Create a regular expression mode.
r = s.match (re); // Try to match the search string.
Return (r); // The array returned contains all "ain"
// Four matching.
}
The following lines of code demonstrate the use of the Match function method in the js of the string text.
var, re = "spain";
r = "The Rain in Spain" .RePlace (RE, "Canada");
The Match () method is used to find the specified value from the string. This method is similar to Indexof () and LastIndexof (). The difference is that it returns the specified value, not the position of the specified value in the string. Indexof () and LastInDEXOF () methods return position numbers if you cannot find a return -1. Pay attention to the case
<script type = "text/javascript">
Var Str = "Hello World!"
document.write (str.match ("world") + "")
document.write (str.match ("world") + "")
document.write (str.match ("worldd") + "")
document.write (str.match ("world!")))
</script>