Example Analysis of exec function usage in JavaScript and exec instance analysis
This example describes how to use the exec function in JavaScript. Share it with you for your reference. The details are as follows:
The exec function in javaScript runs the search in the string in regular expression mode and returns an array containing the search result.
RgExp.exe c (str)
Parameters:
RgExp is required. A regular expression object that contains the regular expression mode and available flag.
Str is required. The String object or String text to be searched.
Note:
If the exec method does not find a match, it returns null. If it finds a match, the exec method returns an array and updates the attributes of the global RegExp object to reflect the matching result. The 0 element of the array contains the complete match, and the 1st to nelement contains any child match in the match. This is equivalent to the match method without setting the global flag.
If a global flag is set for the regular expression, exec searches for it at the position indicated by the value of lastIndex. If the global flag is not set, exec ignores the value of lastIndex and searches from the starting position of the string.
The array returned by the exec method has three attributes: input, index, and lastIndex. The Input attribute contains the entire searched string. The Index attribute contains the position of the matched substring in the searched string. The LastIndex attribute contains the next position of the last character in the match.
<Script language = "JavaScript"> function ge (obj) {re =/(\ w +) \ s (\ d +)/;re.exe c (obj. value); window. alert (RegExp. $1 + "your age is" + this. form1.ag. value + RegExp. $2) ;}</script>
I hope this article will help you design javascript programs.