Translation/5-09-regexp-groups#257
Conversation
|
@leviding 校对认领 |
|
@Moonliujk Great |
|
@leviding 认领校對 |
|
@calpa 👌 |
Moonliujk
left a comment
There was a problem hiding this comment.
翻译的还不错!有些小问题指出来了,望译者注意一下!另:中间有一个代码错误,我指出来了,译者可以和原作者沟通一下便于确认!
| 我们可以添加额外三位 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`。 |
| 但是,还有一种更讨巧的方法:`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}`。 |
There was a problem hiding this comment.
这里我们把正则 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+)?` 可以表示一个正数。 | |||
| 注意,在 JavaScript 中,`pattern:/.../` 中的 `/` 需要被转义。 | ||
|
|
||
| We need a number, an operator, and then another number. And optional spaces between them. | ||
| 我们需要匹配一个数字、一个运算符还有另一个数字。除此以外,还有它们之间的空格。 |
| 完整的正则表达式为:`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+)?)`. | ||
| 为了得到我们所需的数据:数字和运算符,我们需要使用捕获括号。 |
There was a problem hiding this comment.
这个句子没有翻译完。
参考:为了将得到的结果转化为数组,我们须将所需的数据:数字及运算符,包裹在括号中,所得到的表达式为:pattern:(-?\d+(\.\d+)?)\s*([-+*/])\s*(-?\d+(\.\d+)?)。
| 捕获括号将 `pattern:(go)` 划为了一组。 | ||
|
|
||
| Let's make something more complex -- a regexp to match an email. | ||
| 让我们尝试一个更复杂的例子——一个匹配 email 地址的正则表达式。 |
| ``` | ||
|
|
||
| Here we have two matches for `pattern:<(.*?)>`, each of them is an array with the full match and groups. | ||
| 如此我们便得到了 `pattern:<(.*?)>` 的两个匹配项, 他们中的每一个都包括完整的匹配和对应的捕获组。 |
| 之后就是各个捕获组,从左往右依次排开。第一个左括号将会匹配到第一个捕获组 `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]`。 |
| `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`)。** |
| 数组的长度依然是 `3`。但是因为捕获组 `pattern:(z)?` 没有对应项,所以结果为 `["ac", undefined, "c"]`。 | ||
|
|
||
| ## Non-capturing groups with ?: | ||
| ## 非捕获括号 ?: |
|
@Hopsken 有空修改下吧 |
89d173e to
d0e74df
Compare
| 查找三位颜色 `#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 进制数,不多也不少。而这三位可能有,也可能没有。 |
There was a problem hiding this comment.
这里用『无论』感觉会比较奇怪。保留不改。
There was a problem hiding this comment.
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`。 |
| 最简单的方式 —— 直接附加上去:`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`。 |
There was a problem hiding this comment.
『讨巧』中文意思为『取巧,不费力气而得到好处』,用在这里我认为并没有什么不妥。翻译是给人看的,如果非要照字典的话,为什么不用机翻?
There was a problem hiding this comment.
讨巧一词,讨为动词,有渴求,请求的意思,我认为已经上下级的身份不对了。
@Moonliujk 请帮忙校对。
| 注意,在 JavaScript 中,`pattern:/.../` 中的 `/` 需要被转义。 | ||
|
|
||
| We need a number, an operator, and then another number. And optional spaces between them. | ||
| 我们需要匹配一个数字、一个运算符还有另一个数字。除此以外,还有它们之间可能存在的空格。 |
There was a problem hiding this comment.
我们需要匹配一个数字、一个运算符,还有另一个数字,以及它们之间可能存在的空格。
还有另一个数字,以及它们之间可能存在的空格。
这里自己选择是用,还是用。,逗号会比较通畅一点。
There was a problem hiding this comment.
如果要说通顺的话,我认为我的译法会更通顺。否则,又是『还有』,又是『以及』的,读起来不是更累吗?
|
@leviding 校对完成 |
|
没什么问题了,我抽时间看一下就 merge 了,各位辛苦 👍 |
翻译完成 #238