GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Catastrophic backtracking by Isildur46 · Pull Request #820 · javascript-tutorial/zh.javascript.info

Conversation

@Isildur46

@Isildur46 Isildur46 commented Aug 31, 2020

Copy link
Copy Markdown
Contributor

目标章节:9-regular-expressions/15-regexp-catastrophic-backtracking

当前上游最新 commit:javascript-tutorial/en.javascript.info@32e20fc#diff-daa73ffcb00f33284c228961daab9209

本 PR 所做更改如下:

文件名 参考上游 commit 更改(理由)
article.md javascript-tutorial/en.javascript.info@32e20fc#diff-daa73ffcb00f33284c228961daab9209 首次翻译

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Isildur46
翻译 Catastrophic backtracking
018f187
@javascript-translate-bot javascript-translate-bot added the review needed label Aug 31, 2020
ghost
ghost suggested changes Sep 5, 2020
View reviewed changes

@ghost ghost left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Isildur46 感谢翻译!一点格式上的小问题,请再查看一下~

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
开发者们很容易一不小心就写出这类正则表达式,所以我们迟早会面对这种意外问题。

The typical symptom -- a regular expression works fine sometimes, but for certain strings it "hangs", consuming 100% of CPU.
典型的症状就是 -- 一个正则表达式有时能正常工作,但在某些特定字符串中就会消耗 100% 的 CPU 算力,出现「挂起」现象,。

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
典型的症状就是 -- 一个正则表达式有时能正常工作,但在某些特定字符串中就会消耗 100% 的 CPU 算力,出现「挂起」现象,。
典型的症状就是 - 一个正则表达式有时能正常工作,但在某些特定字符串中就会消耗 100% 的 CPU 算力,出现「挂起」现象。

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
## 例子

Let's say we have a string, and we'd like to check if it consists of words `pattern:\w+` with an optional space `pattern:\s?` after each.
比如我们现在有一个字符串,我们想检查其中是否包含一些字符 `pattern:\w+` ,我们允许字符后跟着可选的空格符 `pattern:\s?` 。

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
比如我们现在有一个字符串,我们想检查其中是否包含一些字符 `pattern:\w+` ,我们允许字符后跟着可选的空格符 `pattern:\s?` 。
比如我们现在有一个字符串,我们想检查其中是否包含一些字符 `pattern:\w+`,我们允许字符后跟着可选的空格符 `pattern:\s?`。

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ShiYuanjun-Tim ShiYuanjun-Tim Sep 18, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

能否翻译如下,因为根据下面的例子来看这里的words应该理解为单词更贴切:
比如我们现在有一个字符串,我们想检查该字符串是不是由一些后面带有可选空格pattern:\s?的单词pattern:\w+所组成 。

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
比如我们现在有一个字符串,我们想检查其中是否包含一些字符 `pattern:\w+` ,我们允许字符后跟着可选的空格符 `pattern:\s?` 。

We'll use a regexp `pattern:^(\w+\s?)*$`, it specifies 0 or more such words.
我们使用一个这样的正则 `pattern:^(\w+\s?)*$` ,它指定了 0 个或更多个此类的字符。

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
我们使用一个这样的正则 `pattern:^(\w+\s?)*$` ,它指定了 0 个或更多个此类的字符。
我们使用一个这样的正则 `pattern:^(\w+\s?)*$`,它指定了 0 个或更多个此类的字符。

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
为了理解它,我们来简化一下例子:移除空格符 `pattern:\s?`,使其成为 `pattern:^(\w+)*$`。

And, to make things more obvious, let's replace `pattern:\w` with `pattern:\d`. The resulting regular expression still hangs, for instance:
同时为了让问题更显著,再用 `pattern:\d` 替换掉 `pattern:\w` 。这个新的正则表达式执行时仍然会挂起,比如:

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
同时为了让问题更显著,再用 `pattern:\d` 替换掉 `pattern:\w` 。这个新的正则表达式执行时仍然会挂起,比如:
同时为了让问题更显著,再用 `pattern:\d` 替换掉 `pattern:\w`。这个新的正则表达式执行时仍然会挂起,比如:

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
```

The engine tries to match `pattern:$` again, but fails, because meets `subject:!`:
引擎再次去尝试匹配 `pattern:$` ,但是失败了,因为它遇到了 `subject:!` :

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
引擎再次去尝试匹配 `pattern:$` ,但是失败了,因为它遇到了 `subject:!` :
引擎再次去尝试匹配 `pattern:$`,但是失败了,因为它遇到了 `subject:!` :

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
现在星号 `pattern:*` 跟在 `pattern:\w+\s` 后面,而不是 `pattern:\w+\s?` 后面。 这意味着它无法匹配一个拥有多个连续单字字符串 `pattern:\w+` 的单词,也就省下了原本去尝试这些排列组合的时间。

For example, the previous pattern `pattern:(\w+\s?)*` could match the word `subject:string` as two `pattern:\w+`:
举个例子,之前那个模式 `pattern:(\w+\s?)*` 可能以两个 `pattern:\w+` 的方式来匹配单词 `subject:string` :

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
举个例子,之前那个模式 `pattern:(\w+\s?)*` 可能以两个 `pattern:\w+` 的方式来匹配单词 `subject:string` :
举个例子,之前那个模式 `pattern:(\w+\s?)*` 可能以两个 `pattern:\w+` 的方式来匹配单词 `subject:string`:

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
```

The previous pattern, due to the optional `pattern:\s` allowed variants `pattern:\w+`, `pattern:\w+\s`, `pattern:\w+\w+` and so on.
之前那个模式,由于存在可选的 `pattern:\s` ,它允许 `pattern:\w+`,`pattern:\w+\s`,`pattern:\w+\w+` 等等的变体形式。

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
之前那个模式,由于存在可选的 `pattern:\s` ,它允许 `pattern:\w+`,`pattern:\w+\s`,`pattern:\w+\w+` 等等的变体形式。
之前那个模式,由于存在可选的 `pattern:\s`,它允许 `pattern:\w+`、`pattern:\w+\s` 和 `pattern:\w+\w+` 等等的变体形式。

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
有些正则表达式我们人眼一看就知道无法匹配成功,但正则引擎还是会硬去尝试很多它的排列组合。

E.g. in the regexp `pattern:(\d+)*$` it's obvious for a human, that `pattern:+` shouldn't backtrack. If we replace one `pattern:\d+` with two separate `pattern:\d+\d+`, nothing changes:
比如,正则 `pattern:(\d+)*$` 中 `pattern:+` 对于我们人类来说很明显不应去回溯,就算我们用两个独立的 `pattern:\d+\d+` 去替换一个 `pattern:\d+`, 也是根本没作用的:

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
比如,正则 `pattern:(\d+)*$` 中 `pattern:+` 对于我们人类来说很明显不应去回溯,就算我们用两个独立的 `pattern:\d+\d+` 去替换一个 `pattern:\d+`, 也是根本没作用的:
比如,正则 `pattern:(\d+)*$` 中 `pattern:+` 对于我们人类来说很明显不应去回溯,就算我们用两个独立的 `pattern:\d+\d+` 去替换一个 `pattern:\d+`,也是根本没作用的:

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
为什么?因为前瞻断言查找到一个单词 `pattern:\w+`,将其作为一个整体,然后进行捕获形成 `pattern:\1` 。所以我们最终实现了一种占有型加号 `pattern:+` 量词。它只将 `pattern:\w+` 作为一个整体来捕获,而不会只捕获它的某一部分。

For instance, in the word `subject:JavaScript` it may not only match `match:Java`, but leave out `match:Script` to match the rest of the pattern.
例如,在单词 `subject:JavaScript` 不仅可以匹配 `match:Java`,而且可以忽略 `match:Script` ,匹配模式的其余部分。

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
例如,在单词 `subject:JavaScript` 不仅可以匹配 `match:Java`,而且可以忽略 `match:Script` ,匹配模式的其余部分。
例如,在单词 `subject:JavaScript` 中不仅可以匹配 `match:Java`,而且可以忽略 `match:Script`,匹配模式的其余部分。

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread 9-regular-expressions/15-regexp-catastrophic-backtracking/article.md Outdated
```

Here `pattern:\2` is used instead of `pattern:\1`, because there are additional outer parentheses. To avoid messing up with the numbers, we can give the parentheses a name, e.g. `pattern:(?<word>\w+)`.
这里我们用 `pattern:\2` 代替 `pattern:\1`,因为这里附加了额外的外部括号。为了防止数字产生混淆,,我们可以给括号命名,例如 `pattern:(?<word>\w+)`。

@ghost ghost Sep 5, 2020

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
这里我们用 `pattern:\2` 代替 `pattern:\1`,因为这里附加了额外的外部括号。为了防止数字产生混淆,,我们可以给括号命名,例如 `pattern:(?<word>\w+)`。
这里我们用 `pattern:\2` 代替 `pattern:\1`,因为这里附加了额外的外部括号。为了防止数字产生混淆,我们可以给括号命名,例如 `pattern:(?<word>\w+)`。

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@javascript-translate-bot javascript-translate-bot removed the review needed label Sep 5, 2020
@javascript-translate-bot

javascript-translate-bot commented Sep 5, 2020

Copy link
Copy Markdown

Please make the requested changes. After it, add a comment "/done".
Then I'll ask for a new review 👻

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@javascript-translate-bot javascript-translate-bot added the changes requested label Sep 5, 2020
Isildur46 added 2 commits September 7, 2020 13:43
@Isildur46
根据校对意见修改格式
8d2c790
@Isildur46
去除多余空格
4cee7a6
@Isildur46

Isildur46 commented Sep 7, 2020

Copy link
Copy Markdown
Contributor Author

格式已修正,更新了 PR。 @bemself

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Isildur46

Isildur46 commented Sep 7, 2020

Copy link
Copy Markdown
Contributor Author

/done

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@javascript-translate-bot javascript-translate-bot added review needed and removed changes requested labels Sep 7, 2020
@Isildur46
优化翻译
c4ae771
@Isildur46

Isildur46 commented Sep 19, 2020

Copy link
Copy Markdown
Contributor Author

/done
已修正翻译

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@javascript-translate-bot
javascript-translate-bot requested a review from a team September 19, 2020 06:23
@leviding
Update article.md
9c85cdb
@leviding
leviding merged commit b52446b into javascript-tutorial:master Nov 16, 2020
@javascript-translate-bot javascript-translate-bot mentioned this pull request Nov 16, 2020
Chinese Translation Progress #324
Closed
@javascript-translate-bot

javascript-translate-bot commented Nov 16, 2020

Copy link
Copy Markdown

Thank you 💖 I updated the Progress Issue #324 🎉 🎉 🎉

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@leviding

leviding commented Nov 16, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

感谢翻译和校对 👍 后面的内容有些更新和变动,我先合并了,后续再做更新优化。欢迎继续 PR 更多内容。

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@leviding leviding added DONE and removed review needed labels Nov 16, 2020
@leviding leviding mentioned this pull request Nov 18, 2020
优化教程翻译 #853
Closed
17 of 28 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers
@ShiYuanjun-Tim ShiYuanjun-Tim ShiYuanjun-Tim left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

DONE

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@Isildur46 @javascript-translate-bot @leviding @ShiYuanjun-Tim
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.