JavaScript Regular Expression object method comparison of compile () exec () test ()

Source: Internet
Author: User

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" &GT;&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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.