Javascript-Common regular function (for forgetting to see)

Source: Internet
Author: User

Test : Tests whether a string contains a matching result, contains a return of true, and does not contain a return of false.

<script type= "Text/javascript" >      var str = "bbs.byr.cn";       var reg =/b/;       var ret = reg.test (str);       // true   </script>  

match: Regular match based on pattern, if matched to, returns match result, such as no match to return null

<script type= "Text/javascript" >      var str = "This isn ' t me";        var reg =/is/ig;       var ret = str.match (reg);       // is, is      // [' Is ', ' is ']    </script>   

Search : a regular match based on pattern, and returns the number of indexes if it matches a result; 1

<script type= "Text/javascript" >      var str = "This isn ' t me";       var reg =/is/;       var ret = str.search (reg);      alert (ret); // 2   </script>  

Replace: Regular match according to pattern, replace the match result with replacement, the second parameter can be changed to function.

 <script type= "Text/javascript" > var  str = "I love china!"       var  pattern =/i/G;       var  ret = str.replace (pattern, "I" ); alert (ret);   // i Love china!  </script> 
<script type= "Text/javascript" >    var argv = ["1", "2"];     var str = "%s,%s";     var i = 0;     function () {        return argv[i++];    }); </script>

Split: Regular segmentation According to pattern, returns a segmented array

<script type= "Text/javascript" >      var  str = ' http://www.baidu.com/';       var  reg =/\w/;       var  ret = str.split (reg);       // ["http", "", "", "www", "Baidu", "com", "" "   ) </script>  

exec: string is processed in regular order, and the matching result is returned. Array[0] is the original string, Array[i] is the position of the match within the entire searched string. you can use the while (arr = re.exec (str)) = null) to find all .

<script type= "Text/javascript" >      var str = "I love china!" ;        var reg =/i\b/g;        var ret = reg.exec (str);        // I      // ["I", index:9, Input: "I love china!"]    </script>  

Javascript-Common regular function (for forgetting to see)

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.