The compile () method is used to compile regular expressions during script execution, and can also be used to change and recompile regular expressions.
The exec () method is used to retrieve the matching of regular expressions in a string. Returns an array if found, and returns null if not found.
The test () method is used to detect whether a string matches a pattern. Returns TRUE or FALSE.
Grammar:
Compile ():
Regexpobject.compile (regexp,modifier) regexp regular expression. modifier Specifies the type of match. "G" for global match, "I" for case sensitivity, "GI" for global case-sensitive matching. Regexpobject.exec (String) string the string to retrieve. Regexpobject.test (String) string the string to be detected. <script type= "Text/javascript" > var str= "Every man in The world! every woman on earth! "; patt=/man/g; str2=str.replace (Patt, "person"); document.write (str2+ "<br />");p att=/(wo) man/g;patt.compile (patt); Str2=str.replace (Patt, " Person "); document.write (str2);</script> output Result: every person in the world! Every woperson on earth! Every person in the world! Every person on earth!< Script type= "Text/javascript" >&NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;STR&Nbsp;= "Good jjdky"; var patt = new regexp ("Jjdky", " G "); var result; while ((Result = patt.exec ( STR) != null) { document.write (Result); document.write ("<br />"); document.write (patt.lastindex); }</script > Output: jjdky 10<script type= "text/ JavaScript "> var str = " Good jjdky "; var patt1 = new regexp ("Jjdky"); var result = Patt1.test (str); document.write ("result: " + result);</script> Output Result: result: true
This article is from the "First day of the Application Blog" blog, please be sure to keep this source http://6965535.blog.51cto.com/6955535/1874921
JavaScript Regular Expression object method comparison of compile () exec () test ()