JavaScript Learning Notes (iii)

Source: Internet
Author: User

//==regexp===
1. The test () method of the regular expression
varresult;varstr = "Bcatasdsrtophe" ;varRe11 =/cat/g;//Match Catresult =re11.test (str);varRe12 =NewREGEXP ("Cat", "GI");//match Cat is case insensitiveresult =re12.test (str);varRe31 =NewRegExp (". he$");//A string ending with the Heresult =re31.test (str); alert (result);

Text = "000-01-2131";
var pattern41 =/\d{3}-\d{2}-\d{4}/;
if (pattern41.test (text)) {
Alert ("The Pattern was matched.");
}

 //2. Regular expression exec () method 
var text = "Mom and Dad and baby" ; var pattern =/mom (and dad (and)?)? /GI; var matches = pattern.exec (text); Alert (matches ); // mom and dad And,and Dad And,and

Input and Index property
var text = "Cat,bat,sat,fat"; var pattern1 =/.at/; var matches =//cat//0;index property, match in character creation position // Cat // Cat,bat,sat,fat;input Property, a string that applies a regular expression // 0

Pattern's global mode and non-global mode

var text= "Cat,bat,fat,sat"; var // Global Mode var matches;  for (var i=0;i<4;i++= pattern2.exec (text); alert (matches+ "+Pattern2.lastindex);} // when global mode is used, each call to EXEC () returns the next occurrence in the string until the end of the string is searched.  // and in global mode, the Lastindex property of the pattern increases after each call to exec () and remains unchanged in non-global mode. 

JavaScript Learning Notes (iii)

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.