You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ let str = "An input string that takes a long time or even makes this regexp hang
37
37
alert( regexp.test(str) );
38
38
```
39
39
40
-
To be fair, let's note that some regular expression engines can handle such a search effectively. But most of them can't. Browser engines usually hang.
40
+
To be fair, let's note that some regular expression engines can handle such a search effectively. But most of them can't. Browser engines usually hang (Google Chrome can handle such a search since version 88 due to a new experimental regular expression engine shipped in V8, JavaScript engine for browsers and Node.js).
41
41
42
42
## Simplified example
43
43
@@ -74,7 +74,7 @@ Here's what the regexp engine does:
74
74
```
75
75
76
76
After all digits are consumed, `pattern:\d+` is considered found (as `match:123456789`).
77
-
77
+
78
78
Then the star quantifier `pattern:(\d+)*` applies. But there are no more digits in the text, so the star doesn't give anything.
79
79
80
80
The next character in the pattern is the string end `pattern:$`. But in the text we have `subject:z` instead, so there's no match:
@@ -220,7 +220,7 @@ The time needed to try a lot of (actually most of) combinations is now saved.
220
220
221
221
## Preventing backtracking
222
222
223
-
It's not always convenient to rewrite a regexp though. In the example above it was easy, but it's not always obvious how to do it.
223
+
It's not always convenient to rewrite a regexp though. In the example above it was easy, but it's not always obvious how to do it.
224
224
225
225
Besides, a rewritten regexp is usually more complex, and that's not good. Regexps are complex enough without extra efforts.
226
226
@@ -246,7 +246,7 @@ Possessive quantifiers are in fact simpler than "regular" ones. They just match
246
246
247
247
There are also so-called "atomic capturing groups" - a way to disable backtracking inside parentheses.
248
248
249
-
...But the bad news is that, unfortunately, in JavaScript they are not supported.
249
+
...But the bad news is that, unfortunately, in JavaScript they are not supported.
250
250
251
251
We can emulate them though using a "lookahead transform".
0 commit comments