Skip to content

Commit 56365ae

Browse files
committed
minor
1 parent 50d7cc2 commit 56365ae

File tree

1 file changed

+4
-2
lines changed
  • 9-regular-expressions/15-regexp-infinite-backtracking-problem

1 file changed

+4
-2
lines changed

9-regular-expressions/15-regexp-infinite-backtracking-problem/article.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ We want to find all tags, with or without attributes -- like `subject:<a href=".
2626

2727
In particular, we need it to match tags like `<a test="<>" href="#">` -- with `<` and `>` in attributes. That's allowed by [HTML standard](https://html.spec.whatwg.org/multipage/syntax.html#syntax-attributes).
2828

29-
Now we can see that a simple regexp like `pattern:<[^>]+>` doesn't work, because it stops at the first `>`, and we need to ignore `<>` if inside an attribute.
29+
A simple regexp like `pattern:<[^>]+>` doesn't work, because it stops at the first `>`, and we need to ignore `<>` if inside an attribute:
3030

3131
```js run
3232
// the match doesn't reach the end of the tag - wrong!
@@ -41,7 +41,7 @@ To correctly handle such situations we need a more complex regular expression. I
4141

4242
If we substitute these into the pattern above and throw in some optional spaces `pattern:\s`, the full regexp becomes: `pattern:<\w+(\s*\w+="[^"]*"\s*)*>`.
4343

44-
That regexp is not perfect! It doesn't yet support all details of HTML, for instance unquoted values, and there are other ways to improve, but let's not add complexity. It will demonstrate the problem for us.
44+
That regexp is not perfect! It doesn't support all the details of HTML syntax, such as unquoted values, and there are other ways to improve, but let's not add complexity. It will demonstrate the problem for us.
4545

4646
The regexp seems to work:
4747

@@ -222,6 +222,8 @@ The string has no `>` at the end, so the match is impossible, but the regexp eng
222222
...
223223
```
224224

225+
As there are many combinations, it takes a lot of time.
226+
225227
## How to fix?
226228

227229
The backtracking checks many variants that are an obvious fail for a human.

0 commit comments

Comments
 (0)