Regular formula (ii)

Source: Internet
Author: User

Continue to share in the previous chapter.


/* Use the grouping pattern to match */var pattern =/8 (. *) 8/;//get 8. Any character between 8 var str = ' Goo8gle8 '; Str.match (pattern); alert (regexp.$1);//gle, get the string contents of the first grouping var pattern =/8 (. *) 8/;//get 8. Any character between 8 var str = ' Goo8gle8 '; var result =str.replace (pattern, ' <strong>$1</strong> ');// Gets the replacement string output document.write (result);

Run Results :

var pattern =/(. *) \s (. *)/;var str = ' Google Baidu '; var result =str.replace (Pattern, ' $ $ $ ');//replace two grouped values with output document.write (result);

Run Results :

Greed and inertia

/* About greed and inertia */var pattern =/[a-z]+?/;//? Closed greedy match, only replaced by a var str = ' Abcdefjh '; var result =str.replace (pattern, ' xxx '); alert ( result);

Run Results :

Varpattern =/8 (. +?) 8/;/ /Prohibit greedy, open global (another forbidden greedy/8 ([^8]*) 8/) var str = ' A8b8c8d8e8fjh '; var result =str.replace (pattern, ' <strong>$1</ Strong> ');d ocument.write (result);

Run Results :

/* Use EXEC to return array */var pattern =/^[a-z]+\s[0-9]{4}$/i;//beginning to end of line match var str = ' Google + '; alert (pattern.exec (str));//Return entire string var pattern =/^[a-z]+/i;//only matches the letter var str = ' Google + '; alert (pattern.exec (str));//returns Google var pattern =/^ ([a-z]+) \s ([ 0-9]{4}) $/i;//use the grouping var str = ' Google + '; alert (pattern.exec (str) [0]);//Return Google2015alert (Pattern.exec (str) [1]);// Returns Googlealert (Pattern.exec (str) [2]);//returns 2015//capture grouping and non-capturing grouping */var pattern =/(\d+) ([a-z]+)/;//capturing grouping var str = ' 2015google '; alert (pattern.exec (str));

Run Results :

var pattern =/(\d+) (?: [A-z])/;//non-capturing grouping var str = ' 2015google '; alert (pattern.exec (str));

Run Results :

/* Use Group nesting */var pattern =/(A? ( B? (C?))) /;//from outside to get var str = ' ABC '; alert (pattern.exec (str));

Run Results :

/* Use forward capture */var pattern =/(goo (? =gle))/;//goo must follow gle to capture var str = ' Google '; alert (pattern.exec (str));//goo,goo/* Use special characters to match */var pattern =/\.\[\/b\]/;//special characters, escaped with \ symbol var str = '. [/b] '; Alert (pattern.exec (str));//. [/b]/* Use newline mode */var pattern =/^\d+/mg;//enable line break mode var str = ' 1.baidu\n2.google\n3.bing '; var result =str.replace (pattern, ' # ' //1/2/3 with ' # ' to replace alert (result);

Run Results :

Common regular

/* Check ZIP code */var pattern =/[1-9][0-9]{5}/;//a total of 6 digits, the first digit cannot be 0var str = ' 165000 '; alert (Pattern.test (str));//true/* Check file Compression Package */ var pattern =/[\w]+\.zip|rar|gz/;//\w All numbers and letters and _var str = ' 123.gz '; alert (Pattern.test (str));//true/* Delete extra spaces */var pattern =/\s/g;var str = ' 1 2 3 '; var result =str.replace (pattern, '), alert (result),//123/* Delete the leading and trailing spaces */var pattern =/^\s+/g;/ /Force first var str = '    1 2 3   '; var result =str.replace (pattern, ');p attern =/\s+$/;//Force tail result =result.replace (pattern , '); Alert (' | ') + result+ ' | '); /123

Run Results ( Mandatory first / mandatory tail ):

/* Simple Email Verification */var pattern =/^ ([a-za-z0-9_\.] +) @ ([a-za-z0-9_\.] +)\. ([a-za-z]{2,4})/;var str = ' [email protected] '; Alert (Pattern.test (str));//true


Regular formula (ii)

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.