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

Source: Internet
Author: User

Learn JavaScript regular Good Article one: http://www.cainiao8.com/web/js_note/js_regular_expression.html

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

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

Match : A regular match based on pattern and, if matched to, returns a matching result, such as a match that does not return a null

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

Search : A regular match based on pattern, or the number of indexes if it matches to a result, or return 1

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

Replace: a regular match based on pattern, replacing the result with a replacement

<script type= "Text/javascript" > 
 var str = "I love china!"; 
 var pattern =/i/g; 
 var ret = str.replace (pattern, "I"); 
 alert (ret); I Love china! 

Split : Regular segmentation based on pattern, return of a segmented array

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

exec : Regular processing of string and return of matching results. Array[0] is the original string, Array[i] to match the position in the entire searched string.

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

The above is a small series to introduce the JavaScript regular expression summary (test|match|search|replace|split|exec), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.