METHOD:
RegExp::exec
object.exec([str])
object([str])
This method executes a search for a match in a specified string, returning
a result array. (If, however, you simply want to test whether or not
there is a match, it is best to use the test
method or the String.search method.)
For example, the following blocks of code, one for the Internet Explorer
browser and the other for Netscape, each execute a search of the string
"the fisherman" for a match with the regular expression 'rexp'. If one
is found (and in the case of the example it is), then an appropriate
message is printed.
Code:
rexp = /[aeiou]/g
myString = "the fisherman"
if(rexp.exec(myString))
match = rexp.exec(myString)
document.write("Successfully matched " + match)
Code:
rexp = /[aeiou]/g
myString = "the fisherman"
if(rexp(myString))
match = RegExp.lastMatch
document.write("Successfully matched " + match)
Output:
Successfully matched e
NOTE:
The Netscape browser can call the exec method both directly
(object.exec([str])) or indirectly (object([str])), whereas
Microsoft Internet Explorer can only call it directly. If no string
is declared then the value of RegExp.input
is used. If the search fails, the exec method returns null.
|