Skip to content

Commit f19b492

Browse files
committed
minor fixes
1 parent 2b1d670 commit f19b492

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

15-regexp-catastrophic-backtracking/article.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ If you run the example below, you probably won't see anything, as JavaScript wil
3131

3232
```js run
3333
let regexp = /^(\w+\s?)*$/;
34-
let str = "An input string that takes a long time or even makes this regexp to hang!";
34+
let str = "An input string that takes a long time or even makes this regexp hang!";
3535

3636
// will take a very long time
3737
alert( regexp.test(str) );
@@ -41,7 +41,7 @@ To be fair, let's note that some regular expression engines can handle such a se
4141

4242
## Simplified example
4343

44-
What's the matter? Why the regular expression hangs?
44+
What's the matter? Why does the regular expression hang?
4545

4646
To understand that, let's simplify the example: remove spaces `pattern:\s?`. Then it becomes `pattern:^(\w+)*$`.
4747

@@ -60,7 +60,7 @@ So what's wrong with the regexp?
6060

6161
First, one may notice that the regexp `pattern:(\d+)*` is a little bit strange. The quantifier `pattern:*` looks extraneous. If we want a number, we can use `pattern:\d+`.
6262

63-
Indeed, the regexp is artificial, we got it by simplifying the previous example. But the reason why it is slow is the same. So let's understand it, and then the previous example will become obvious.
63+
Indeed, the regexp is artificial; we got it by simplifying the previous example. But the reason why it is slow is the same. So let's understand it, and then the previous example will become obvious.
6464

6565
What happens during the search of `pattern:^(\d+)*$` in the line `subject:123456789z` (shortened a bit for clarity, please note a non-digit character `subject:z` at the end, it's important), why does it take so long?
6666

@@ -111,7 +111,7 @@ Here's what the regexp engine does:
111111
```
112112
113113
114-
4. There's no match, so the engine will continue backtracking, decreasing the number of repetitions. Backtracking generally works like this: the last greedy quantifier decreases the number of repetitions until it can. Then the previous greedy quantifier decreases, and so on.
114+
4. There's no match, so the engine will continue backtracking, decreasing the number of repetitions. Backtracking generally works like this: the last greedy quantifier decreases the number of repetitions until it reaches the minimum. Then the previous greedy quantifier decreases, and so on.
115115
116116
All possible combinations are attempted. Here are their examples.
117117
@@ -196,7 +196,7 @@ This regexp is equivalent to the previous one (matches the same) and works well:
196196
197197
```js run
198198
let regexp = /^(\w+\s)*\w*$/;
199-
let str = "An input string that takes a long time or even makes this regex to hang!";
199+
let str = "An input string that takes a long time or even makes this regex hang!";
200200
201201
alert( regexp.test(str) ); // false
202202
```
@@ -254,7 +254,7 @@ We can emulate them though using a "lookahead transform".
254254

255255
So we've come to real advanced topics. We'd like a quantifier, such as `pattern:+` not to backtrack, because sometimes backtracking makes no sense.
256256

257-
The pattern to take as much repetitions of `pattern:\w` as possible without backtracking is: `pattern:(?=(\w+))\1`. Of course, we could take another pattern instead of `pattern:\w`.
257+
The pattern to take as many repetitions of `pattern:\w` as possible without backtracking is: `pattern:(?=(\w+))\1`. Of course, we could take another pattern instead of `pattern:\w`.
258258

259259
That may seem odd, but it's actually a very simple transform.
260260

@@ -293,7 +293,7 @@ let regexp = /^((?=(\w+))\2\s?)*$/;
293293

294294
alert( regexp.test("A good string") ); // true
295295

296-
let str = "An input string that takes a long time or even makes this regex to hang!";
296+
let str = "An input string that takes a long time or even makes this regex hang!";
297297

298298
alert( regexp.test(str) ); // false, works and fast!
299299
```
@@ -304,7 +304,7 @@ Here `pattern:\2` is used instead of `pattern:\1`, because there are additional
304304
// parentheses are named ?<word>, referenced as \k<word>
305305
let regexp = /^((?=(?<word>\w+))\k<word>\s?)*$/;
306306

307-
let str = "An input string that takes a long time or even makes this regex to hang!";
307+
let str = "An input string that takes a long time or even makes this regex hang!";
308308

309309
alert( regexp.test(str) ); // false
310310

0 commit comments

Comments
 (0)