JS Regular learning the NFA engine of little Remember

Source: Internet
Author: User
Tags posix

Before always think oneself is also good, in look at the times of cobalt carbonate, Barret Lee and other great gods are playing the perfect after the superb found I was only a war five slag.

To embrace the thigh, to seek the great God,

I had an impression that there were a lot of engines, but I didn't know what the engines were.

Today read "Proficient in regular expression" only to find traditional nfa,posix NFA and DFA (specific Baidu under it).
Can be used for such a long time, still do not know what kind of JS belongs to it.
In "Mastering regular Expressions" There is a simple method of detecting which one belongs.

Use /nfa|nfa not/ to match "NFA not".
If the match results are ' NFA ', then this is the traditional NFA (the traditional NFA).
If it's ' NFA not ', it could be a POSIX NFA or a DFA.

Let's try the regular first.

" NFA not ". Match (/nfa|nfa not/);

The result is the NFA, then you can be sure that JS engine is the traditional NFA.


If it is ' NFA not ', then proceed to the next test to determine whether it is a POSIX NFA or a DFA.
/x (. +) +x/ to match "==xx=============================".
If the execution time is long, the NFA (the traditional NFA can already be determined in the previous step).
If the execution time is short, the basic is the DFA, also may be the advanced optimized NFA.
In addition, if the execution of overflow, time-out, it can certainly be the NFA.

Let's try this, okay?

Console.  Time ('/x (. +) +x/test '); "==xx=============================". Match (/x (. +) +x/); console. Timeend ('/x (. +) +x/test ');

/x (. +) +x/test: 17300.000ms
Oh, my God, a little regular match for 17 seconds, I scared the urine. (This result is related to the computer configuration, your God machine may be faster than I n times)

Of course, I can't explain it now, I can only tell you this seems to be the NFA engine's backtracking runaway caused.
So we can use this method to detect the NFA and the DFA.

NFA is the expression of the dominant engine, DFA is the text-driven engine, and we have learned that JS is a traditional NFA, then we will be around this NFA in-depth study.

Today's sharing is these, I will go to practice, and continue to share tomorrow.

 

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.