[Technical Learning] Regular expression analysis

Source: Internet
Author: User
Tags rtrim

jquery source Learning, the use of a large number of regular expressions found, so the common regular expressions are decomposed, and examples of their matching rules

First, match the commonly used number format, including positive and negative integer decimals and can count the method
Core_pnum =/[+-]? (?:\ d*\.|) \d+ (?: [ee][+-]?\d+|) /.source,//used for matching numbers

1, [+-]? Indicates that the first digit is a + or-number, or it can have no

2, (?: \ d*\.|) : Use the?: to indicate that the grouping will not be snapped, there are 0 to multiple digits, a decimal number, | The match is empty., that is, match 12. Format or empty

3. \d+: 1 to multiple digits

4, (?: [ee][+-]?\d+|) indicates that the packet is not captured, matches E or e,0-1 +--number, multiple numbers, indicating a matching e+3 format or an empty

Can be matched to a number format, because with. Source as a token, get the backup string, you can make a complete regular with other strings, for use, mainly used as:, for example:

    var core_pnum =/[+-]? (?:\ d*\.|) \d+ (?: [ee][+-]?\d+|) /.source,//  used for matching numbers        new RegExp ("^ (" + Core_pnum + ") (. *) $", "I"),// ^([+-]? (?:\ d*\.|) \d+ (?: [ee][+-]?\d+|)) (. *) $        new RegExp ("^ (" + Core_pnum + ") (?! PX) [a-z%]+$, "I" ),//^ ([-+]? (?: \ d*\.|) \d+ (?: [ee][+-]?\d+|)) (?! PX) [a-z%]+$        new RegExp ("^ ([+ +]) = (" + core_pnum + ")", "I" )        ,new RegExp ("^ (?:( [+-]) =|) ("+ Core_pnum +") ([a-z%]*) $ "," I "),
    varCore_pnum =/[+-]? (?:\ d*\.|) \d+ (?: [ee][+-]?\d+|) /.source,//used for matching numbers        ///^([+-]?(?:\ d*\.|) \d+ (?: [ee][+-]?\d+|)) (.*)$        //You can separate numbers from other characters, for example: 100px, separated by 100 and PXRnumsplit =NewRegExp ("^ (" + Core_pnum + ") (. *) $", "I" ),        //^([+-]?(?:\ d*\.|) \d+ (?: [ee][+-]?\d+|)) (?! PX) [a-z%]+$        //matches a non-PX-terminated characterRNUMNONPX =NewRegExp ("^ (" + Core_pnum + ") (?! PX) [a-z%]+$ "," I " ),        ///^([+-])=([+-]?(?:\ d*\.|) \d+ (?: [ee][+-]?\d+|)) /IRrelnum =NewRegExp ("^ ([+ +]) = (" + core_pnum + ")", "I" ),        ///^(?:( [+-]) =|) ([+-]? (?:\ d*\.|) \d+ (?: [ee][+-]?\d+|)) ([a-z%]*) $/iRfxnum =NewRegExp ("^ (?:( [+-]) =|) ("+ Core_pnum +") ([a-z%]*) $ "," I " ); varWidth= ' 100px '; Alert (Width.match (Rnumsplit));//[' 100px ', ' + ', ' px ']Alert (Width.match (RNUMNONPX));//NULL        varAa= "+=100.12"alert (rrelnum.exec (AA));//[' +=100.12 ', ' + ', ' 100.12 ']

        var core_pnum =/^[+-]? (?:\ d*\.|) \d+ (?: [ee][+-]?\d+|) $/; //        var num1=1.0034e-18;        Alert (Core_pnum.exec (NUM1))//        var num2=43.12        alert (core_ Pnum.exec (num2))//43.12        var num3=43        alert (core_pnum.exec ( num3))//

Two, non-null characters, global match

Core_rnotwhite =/\s+/g,

Use environment for, string cut, convert all strings by character to an array:

        var core_rnotwhite =/\s+/g,            str= ' One ', three ';        Alert (Str.match (core_rnotwhite))//[' One ', ' both ', ' three ']

Third, global match left or right space

RTrim =/^[\s\ufeff\xa0]+| [\s\ufeff\xa0]+$/g

    var rtrim =/^[\s\ufeff\xa0]+| [\s\ufeff\xa0]+$/G,         aa= '       aaa         ' ;    ;    Alert ("1" +aa.replace (RTrim, ') + "1"); // 1aaa11

Iv. matching HTML tags or id selectors #xxx

rquickexpr =/^ (?: \ s* (<[\w\W]+>) [^>]*|# ([\w-]*)] $/,

V. Match a separate HTML tag
rsingletag=/^< (\w+) \s*\/?> (?: <\/\1>|) $/,

Matches a string consisting of],:{} and any character, JSON common string
Rvalidchars =/^[\],:{}\s]*$/,

Seven
Rvalidbraces =/(?: ^|:|,) (?: \ s*\[) +/g,
Rvalidescape =/\\ (?: ["\\\/bfnrt]|u[\da-fa-f]{4})/g,
Rvalidtokens =/"[^" \\\r\n]* "|true|false|null|-? (?:\ d+\.|) \d+ (?: [ee][+-]?\d+|) /g,

Matches dashed string for camelizing
Rmsprefix =/^-ms-/,
Rdashalpha =/-([\da-z])/gi,

[Technical Learning] Regular expression analysis

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.