JavaScript regular-expression learning

Source: Internet
Author: User
Tags regular expression expression engine

Recently spent a week reading the first 6 chapters of proficient Regular expression (3rd edition), hoping to be proficient in regular expressions and able to write "JavaScript depth understanding Regular expression" articles. A week is too short, I think it is only to achieve the "no fear", "more confident" degree, so the goal of this article can only help readers "grasp" the regular expression.

I think the regular expression is difficult, mainly reflected in the following aspects:
1 the symbol of regular expression obscure difficult to understand
2 does not support typesetting (at least JavaScript currently does not support)
3 cannot set breakpoints, can not track debugging
4 There is no real standard, the regular expressions supported by different tools have many differences in detail

Refer to the <<javascript Regular Expression entry notes (full version), if the meaning of the regular expression symbol is not known in the following section >>

Regular expressions are essentially a set of models for handling strings, helping people to implement complex algorithms using short expressions. The early regular expression engine had only 300 lines of code that grew to less than 10,000 lines of code. Make an inappropriate analogy, using regular expressions to handle strings, like using SQL to process data. As we avoid using complex SQL when working with data, we should avoid using complex regular expressions when dealing with strings. Here is a section of code to determine the legality of an IP address, simpler than using regular expressions:

var isipadress = function (ipstr) {     
    var pttrn =/^ (\d{1,3}) \. ( \d{1,3}) \. (\d{1,3}) \. (\d{1,3}) $/;     
    var ipobj = pttrn.exec (IPSTR);     
    var bool_result = false;     
    if (ipobj) {     
        //Add further recognition rules     
        if (ipstr=== "0.0.0.0") {     
            Bool_result = false;     
        } else if (ipstr=== "1.1.1.1") {     
            Bool_result = false;     
        } else if (ipobj[1]>=0 && ipobj[1]<=255 && ipobj[2]>=0 && ipobj[2]<=255 && Ipobj[3]>=0 && ipobj[3]<=255 && ipobj[4]>=0 && ipobj[4]<=255) {     
            Bool_result = true;     
        } else{     
            Bool_result = false;     
        }     
    }     
        
    return bool_result;     
}

Debugging information:

Isipadress ("10.1.6.255") True

Isipadress ("1.1.1.1") false

Isipadress ("10.1.a.255") false

Discussion on the efficiency of regular expression in execution

Execute the following JavaScript regular expression, compare ie/safari/firefox/chrome execution efficiency, find Safari and Firefox are slow to open for the first time, but refresh can be done instantaneously. Chrome and IE are slow to open and refresh each time, which means that JavaScript is optimized for IE and chrome or based on the NFA algorithm, while Safari and Firefox may use the DFA algorithm (or cache).

var p1 =/x (?:. +) +x/;     
Ijs.put (P1.exec ("=xx=========================="));

Regular Expression object: Exec method:

If the regular expression uses g as the suffix, it remembers the result of the last execution, and it will find a later match when it executes.

If the regular expression does not use G as a suffix, the result of each execution is the same, and the first match is taken. If the capture bracket is used, the result of the match can be obtained by 1/2/3/... Suffix is obtained, see the preceding code to determine the legality of an IP address.

var pttrn =/bb/;     
Ijs.showobject (Pttrn.exec ("abaabbaaa"));     
Ijs.showobject (Pttrn.exec ("abaabbaaa"));     
PTTRN =/a+/g;     
Ijs.showobject (Pttrn.exec ("abaabbaaa"));     
Ijs.showobject (Pttrn.exec ("abaabbaaa"));

Debugging information:

[Object] bb
|--[string] 0-------------BB
|--[number] Index-------------4
|--[string] Input-------------abaabbaaa
|--[number] Lastindex-------------6

[Object] bb
|--[string] 0-------------BB
|--[number] Index-------------4
|--[string] Input-------------abaabbaaa
|--[number] Lastindex-------------6

[Object] A
|--[string] 0-------------A
|--[number] Index-------------0
|--[string] Input-------------abaabbaaa
|--[number] lastindex-------------1

[Object] Aa
|--[string] 0-------------AA
|--[number] Index-------------2
|--[string] Input-------------abaabbaaa
|--[number] lastindex-------------4

Related Article

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.