Skip to content

Translation/5-09-regexp-groups#257

Merged
leviding merged 7 commits intojavascript-tutorial:zh-hansfrom
Hopsken:translation/5-9-regexp-group
Oct 20, 2018
Merged

Translation/5-09-regexp-groups#257
leviding merged 7 commits intojavascript-tutorial:zh-hansfrom
Hopsken:translation/5-9-regexp-group

Conversation

@Hopsken
Copy link
Copy Markdown
Contributor

@Hopsken Hopsken commented Oct 13, 2018

翻译完成 #238

@Moonliujk
Copy link
Copy Markdown

@leviding 校对认领

@leviding
Copy link
Copy Markdown
Member

@Moonliujk Great

@calpa
Copy link
Copy Markdown

calpa commented Oct 14, 2018

@leviding 认领校對

@leviding
Copy link
Copy Markdown
Member

@calpa 👌

Copy link
Copy Markdown

@Moonliujk Moonliujk left a comment

Choose a reason for hiding this comment

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

翻译的还不错!有些小问题指出来了,望译者注意一下!另:中间有一个代码错误,我指出来了,译者可以和原作者沟通一下便于确认!

我们可以添加额外三位 16 进制数,不多也不少。而这三位可能有,也可能没有。

The simplest way to add them -- is to append to the regexp: `pattern:/#[a-f0-9]{3}([a-f0-9]{3})?/i`
最简单的方式——直接附加上去:`pattern:/#[a-f0-9]{3}([a-f0-9]{3})?/i`
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

破折号前后需要添加空格:参见 标点符号

但是,还有一种更讨巧的方法:`pattern:/#([a-f0-9]{3}){1,2}/i`

Here the regexp `pattern:[a-f0-9]{3}` is in parentheses to apply the quantifier `pattern:{1,2}` to it as a whole.
这里我们把正则 `pattern:[a-f0-9]{3}` 归为一个捕获组,并且应用量词 `pattern:{1,2}`
Copy link
Copy Markdown

@Moonliujk Moonliujk Oct 14, 2018

Choose a reason for hiding this comment

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

这里我们把正则 pattern:[a-f0-9]{3} 归为一个捕获组
=》
这里我们把正则 pattern:[a-f0-9]{3} 放置在括号内
虽然放置在括号内的正则确实可以成为捕获组,但是我觉得这里还是直译比较好,原因是这里并不是想表达捕获组的含义,仅仅是一种书写规则;

@@ -1,6 +1,6 @@
A positive number with an optional decimal part is (per previous task): `pattern:\d+(\.\d+)?`.
回顾上个问题,`pattern:\d+(\.\d+)?` 可以表示一个正数。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

可以表示一个正数。
=》
可以匹配一个具有可选择小数部分的正数。

注意,在 JavaScript 中,`pattern:/.../` 中的 `/` 需要被转义。

We need a number, an operator, and then another number. And optional spaces between them.
我们需要匹配一个数字、一个运算符还有另一个数字。除此以外,还有它们之间的空格。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

还有它们之间的空格。
=》
还有它们之间可能存在的空格。

完整的正则表达式为:`pattern:-?\d+(\.\d+)?\s*[-+*/]\s*-?\d+(\.\d+)?`

To get a result as an array let's put parentheses around the data that we need: numbers and the operator: `pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)`.
为了得到我们所需的数据:数字和运算符,我们需要使用捕获括号。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

这个句子没有翻译完。
参考:为了将得到的结果转化为数组,我们须将所需的数据:数字及运算符,包裹在括号中,所得到的表达式为:pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)

捕获括号将 `pattern:(go)` 划为了一组。

Let's make something more complex -- a regexp to match an email.
让我们尝试一个更复杂的例子——一个匹配 email 地址的正则表达式。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

破折号前后需要添加空格

```

Here we have two matches for `pattern:<(.*?)>`, each of them is an array with the full match and groups.
如此我们便得到了 `pattern:<(.*?)>` 的两个匹配项, 他们中的每一个都包括完整的匹配和对应的捕获组。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

中间的逗号改为全角

之后就是各个捕获组,从左往右依次排开。第一个左括号将会匹配到第一个捕获组 `result[1]`。在这个例子中,它涵盖了整个标签的内容。

Then in `result[2]` goes the group from the second opening `pattern:(` till the corresponding `pattern:)` -- tag name, then we don't group spaces, but group attributes for `result[3]`.
`result[2]` 对应第二个左括号 `pattern:(` 到与其对应的右括号 `pattern:)` 之间的内容——标签名。再然后,我们跳过空格,将所有属性划为一组,对应 `result[3]`
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

破折号前后需要添加空格

`result[2]` 对应第二个左括号 `pattern:(` 到与其对应的右括号 `pattern:)` 之间的内容——标签名。再然后,我们跳过空格,将所有属性划为一组,对应 `result[3]`

**If a group is optional and doesn't exist in the match, the corresponding `result` index is present (and equals `undefined`).**
**如果某个捕获组是可选的,且在匹配中没有找到对应项,那么在相应的匹配结果中,该项依然会存在(值为`undefined`)。**
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

undefined 前需要添加空格

数组的长度依然是 `3`。但是因为捕获组 `pattern:(z)?` 没有对应项,所以结果为 `["ac", undefined, "c"]`

## Non-capturing groups with ?:
## 非捕获括号 ?:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

非捕获括号
=》
非捕获组

@Moonliujk
Copy link
Copy Markdown

@Hopsken @leviding 校对完毕

@leviding leviding added the enhancement New feature or request label Oct 14, 2018
@leviding
Copy link
Copy Markdown
Member

@Hopsken 有空修改下吧

@Hopsken Hopsken force-pushed the translation/5-9-regexp-group branch from 89d173e to d0e74df Compare October 15, 2018 13:13
@leviding leviding added WIP Work in process and removed enhancement New feature or request labels Oct 16, 2018
Copy link
Copy Markdown

@calpa calpa left a comment

Choose a reason for hiding this comment

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

写得不错,读起来大致流畅。

查找三位颜色 `#abc` 的正则表达式为:`pattern:/#[a-f0-9]{3}/i`

We can add exactly 3 more optional hex digits. We don't need more or less. Either we have them or we don't.
我们可以添加额外三位 16 进制数,不多也不少。而这三位可能有,也可能没有。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

无论是有这三位,或者是没有。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

这里用『无论』感觉会比较奇怪。保留不改。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Either 不是 A and B 的语句,它是一个 A or B 的关系。不应该是 而...,也...,而是两者其一,或者或者

我们可以添加额外三位 16 进制数,不多也不少。而这三位可能有,也可能没有。

The simplest way to add them -- is to append to the regexp: `pattern:/#[a-f0-9]{3}([a-f0-9]{3})?/i`
最简单的方式 —— 直接附加上去:`pattern:/#[a-f0-9]{3}([a-f0-9]{3})?/i`
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

最简单的方式 —— 在正则表达式后附加上去:

最简单的方式 —— 直接附加上去:`pattern:/#[a-f0-9]{3}([a-f0-9]{3})?/i`

We can do it in a smarter way though: `pattern:/#([a-f0-9]{3}){1,2}/i`.
但是,还有一种更讨巧的方法:`pattern:/#([a-f0-9]{3}){1,2}/i`
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

我们可以用一种更加妙的方法:
妙 / 机智 / 有智慧

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

用『讨巧』也可以吧

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自己看辞典:http://www.iciba.com/smarter

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

『讨巧』中文意思为『取巧,不费力气而得到好处』,用在这里我认为并没有什么不妥。翻译是给人看的,如果非要照字典的话,为什么不用机翻?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

讨巧一词,讨为动词,有渴求,请求的意思,我认为已经上下级的身份不对了。

@Moonliujk 请帮忙校对。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

没问题的,翻译风格不同而已

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@calpa 我查了一下,讨巧 有 做事不费力而占便宜 之意,至于你所说的渴求、请求之意我还没有看到出处。所以这里的翻译我觉得没有问题。

注意,在 JavaScript 中,`pattern:/.../` 中的 `/` 需要被转义。

We need a number, an operator, and then another number. And optional spaces between them.
我们需要匹配一个数字、一个运算符还有另一个数字。除此以外,还有它们之间可能存在的空格。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

我们需要匹配一个数字、一个运算符,还有另一个数字,以及它们之间可能存在的空格。

还有另一个数字,以及它们之间可能存在的空格。
这里自己选择是用还是用,逗号会比较通畅一点。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

可以指出原来的译法有什么问题么。。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

不是问题,而是优化。语句可以分割一点,读起来会比较通顺。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

如果要说通顺的话,我认为我的译法会更通顺。否则,又是『还有』,又是『以及』的,读起来不是更累吗?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

请另外一位校对者 @Moonliujk 帮忙校对。

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

两种都行

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@calpa 这里我觉得你和 @Hopsken 的译法都可以啊!前半部分可以采取 @calpa 的译法,添加一个逗号,语义分隔;后半部分通过句号分隔,和原文的逻辑可能更加贴近,即:我们需要匹配一个数字、一个运算符,还有另一个数字。除此以外,还有它们之间可能存在的空格。

@calpa
Copy link
Copy Markdown

calpa commented Oct 16, 2018

@leviding 校对完成

@leviding leviding added enhancement New feature or request and removed enhancement New feature or request labels Oct 16, 2018
@leviding
Copy link
Copy Markdown
Member

没什么问题了,我抽时间看一下就 merge 了,各位辛苦 👍

@leviding leviding merged commit fe1bd95 into javascript-tutorial:zh-hans Oct 20, 2018
@leviding leviding removed WIP Work in process 正在校对 labels Oct 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants