Understanding this issue requires an understanding of how NFA works in RegExp.
Developing an NFA definition may be too difficult a mission for me. A search on the NFA on the wiki will give you a better explanation. Here, just think that NFAs are the search robots that you give.
The crudely implemented NFA is a bit dumb, it just looks forward to one or two tokens that you give. So, in the synthetic example you give, the NFA first looks like \w+ (and not the brackets for grouping).
Because + is a greedy quantifier, that is, it matches as many characters as possible, so the NFA frantically continues to use characters in target . After 30 a s, the NFA meets the end of the line. After that, the NFA understands that it needs to map other tokens in the template . The next token is + . NFA has mapped it, so it goes to \. . This time he fails.
What the NFA does next, you need to throw one character back, trying to match the pattern, truncating the fake \w+ . Thus, the NFA divided target into two groups, 29 a into one \w+ and one end of a . At first, the NFA tries to use the final element a, matching it with the second + , but it still fails when the NFA \. encounters \. . The NFA continues the process above until it gets full compliance, otherwise it will try to use all possible sections.
So, (\w+)+\. instructs the NFA to group target in this way: the target is split into one or more groups, at least one character per group, and the target ends with a period of ".". While the period is not agreed. NFA is trying to use all partitions. So how many sections are there? 2 ^ n, exponent 2. (JUst think inserting a separator between a ). As below
aaaaaaa a aaaaaa aa aaaaaa aa ..... ....... aa aa ... a aaaaa .... a
If the NFA matches \. it will not hurt. But when it does not fit, this expression is doomed to be infinite exponential.
I do not advertise, but Mastering Regular Expression is a good book for understanding the mechanism in RegExp.