JavaScript Regular Expression Summary (test|match|search|replace|split|exec)

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  

Replace: Regular match according to pattern, replacing match result with replacement

<script type= "Text/javascript" >      var str = "I love china!" ;       var pattern =/i/g;       var ret = str.replace (pattern, "I");       // I Love china!   </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.

<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!"]   

JavaScript Regular Expression Summary (test|match|search|replace|split|exec)

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.