Regular Expression (2), expression (2)

Source: Internet
Author: User

Regular Expression (2), expression (2)

Continue with the previous article.


 

/* Use grouping mode to match */var pattern =/8 (. *) 8 //; // get 8 .. any character between 8 var str = 'goo8gle8'; str. match (pattern); alert (RegExp. $1); // gle to obtain the string content var pattern =/8 (. *) 8 //; // get 8 .. any character between 8 var str = 'goo8gle8'; var result = str. replace (pattern, '<strong> $1 </strong>'); // obtain the replaced string and output document. write (result );

Running result:

 

 

Var pattern = /(. *) \ s (. *)/; var str = 'Google baidu'; var result = str. replace (pattern, '$2 $ 1'); // replace the values of the two groups with the output document. write (result );

Running result:

 

 

Greedy and inert

 

 

/* About greed and inertia */var pattern =/[a-z] +? /;//? If greedy match is disabled, only one var str = 'abcdefance' is replaced; var result = str. replace (pattern, 'xxx'); alert (result );

Running result:

 

 

Varpattern =/8 (. + ?) 8 // No greedy, enabled global (another type of No greedy/8 ([^ 8] *) 8/) var str = 'a8b8c8d8e8f112 '; var result = str. replace (pattern, '<strong> $1 </strong>'); document. write (result );

Running result:

 

 

/* Use exec to return the array */var pattern =/^ [a-z] + \ s [0-9] {4} $/I; // match var str = 'Google 55'{alert(pattern.exe c (str) between the beginning and end of the line; // return the entire string var pattern =/^ [a-z] +/I; // match only the letter var str = 'Google 55'{alert(pattern.exe c (str); // return google var pattern =/^ ([a-z] +) \ s ([0-9] {4}) $/I; // use the grouping var str = 'Google 2015'{alert(pattern.exe c (str) [0]); // return google5alert(pattern.exe c (str) [1]); // return googlealert(pattern.exe c (str) [2]); // return 2015/* capturing group and non-capturing Group */var pattern =/(\ d +) ([a-z] + )/; // capture group var str = '55google'{alert(pattern.exe c (str ));

Running result:

 

 

Var pattern =/(\ d + )(? : [A-z]) // non-capturing group var str = '55google'{alert(pattern.exe c (str ));

Running result:

 

 

/* Use grouping nesting */var pattern =/(? (B? (C ?))) // Obtain var str = 'abc'externalert(pattern.exe c (str) from the external directory ));

Running result:

 

 

/* Use forward-looking capture */var pattern =/(goo (? = Gle) //; // goo must be followed by gle to capture var str = 'google'externalert(pattern.exe c (str); // goo, goo/* match with special characters */var pattern = /\. \ [\/B \]/; // special character. Use the \ symbol to escape var str = '. [/bw.'{alert(pattern.exe c (str ));//. [/B]/* use line feed mode */var pattern =/^ \ d +/mg; // enable line feed mode var str = '1. baidu \ n2.google \ n3.bing '; var result = str. replace (pattern, '#'); // 1/2/3 replace alert (result) );

Running result:

 

 

Common Regular Expressions

 

/* Check the zip code */var pattern =/[1-9] [0-9] {5}/; // six digits in total, the first digit cannot be 0var str = '20140901'; alert (pattern. test (str); // true/* Check the compressed file package */var pattern =/[\ w] + \. zip | rar | gz/; // \ w all numbers and letters and _ var str = '123.gz '; alert (pattern. test (str); // true/* remove unnecessary Spaces */var pattern =/\ s/g; var str = '1 2 3'; var result = str. replace (pattern, ''); alert (result); // 123/* Delete spaces at the beginning and end */var pattern =/^ \ s +/g; // force the first var str = '1 2 3'; var result = str. replace (pattern, ''); pattern =/\ s + $/; // force end result = result. replace (pattern, ''); alert ('|' + result + '|'); // 123

Running result (force beginning/end ):

 

 

 

/* Simple email verification */var pattern =/^ ([a-zA-Z0-9 _ \.] +) @ ([a-zA-Z0-9 _ \.] + )\. ([a-zA-Z] {2, 4})/; var str = 'yc60. com@gmail.com '; alert (pattern. test (str); // true


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.