chore(i8n,curriculum): processed translations (#41490)

Co-authored-by: Crowdin Bot <support+bot@crowdin.com>
This commit is contained in:
camperbot
2021-03-14 21:20:39 -06:00
committed by GitHub
parent 5e563329ad
commit 903a301849
262 changed files with 2871 additions and 2708 deletions

View File

@ -1,6 +1,6 @@
---
id: 587d7dba367417b2b2512ba8
title: Check for All or None
title: 检查全部或无
challengeType: 1
forumTopicId: 301338
dashedName: check-for-all-or-none
@ -8,48 +8,50 @@ dashedName: check-for-all-or-none
# --description--
Sometimes the patterns you want to search for may have parts of it that may or may not exist. However, it may be important to check for them nonetheless.
有时,想要搜寻的匹配模式可能有不确定是否存在的部分。 尽管如此,还是想检查它们。
You can specify the possible existence of an element with a question mark, `?`. This checks for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.
为此,可以使用问号 `?` 指定可能存在的元素。 这将检查前面的零个或一个元素。 可以将此符号视为前面的元素是可选的。
For example, there are slight differences in American and British English and you can use the question mark to match both spellings.
例如,美式英语和英式英语略有不同,可以使用问号来匹配两种拼写。
```js
let american = "color";
let british = "colour";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american); // Returns true
rainbowRegex.test(british); // Returns true
rainbowRegex.test(american);
rainbowRegex.test(british);
```
上面的 `test` 都会返回 `true`
# --instructions--
Change the regex `favRegex` to match both the American English (favorite) and the British English (favourite) version of the word.
修改正则表达式 `favRegex` 以匹配美式英语(`favorite`)和英式英语(`favourite`)的单词版本。
# --hints--
Your regex should use the optional symbol, `?`.
你的正则表达式应该使用可选符号 `?`
```js
favRegex.lastIndex = 0;
assert(favRegex.source.match(/\?/).length > 0);
```
Your regex should match `"favorite"`
你的正则表达式应该匹配 `favorite`
```js
favRegex.lastIndex = 0;
assert(favRegex.test('favorite'));
```
Your regex should match `"favourite"`
你的正则表达式应该匹配 `favourite`
```js
favRegex.lastIndex = 0;
assert(favRegex.test('favourite'));
```
Your regex should not match `"fav"`
你的正则表达式不应该匹配 `fav`
```js
favRegex.lastIndex = 0;

View File

@ -1,6 +1,6 @@
---
id: 5c3dda8b4d8df89bea71600f
title: Check For Mixed Grouping of Characters
title: 检查混合字符组
challengeType: 1
forumTopicId: 301339
dashedName: check-for-mixed-grouping-of-characters
@ -8,62 +8,63 @@ dashedName: check-for-mixed-grouping-of-characters
# --description--
Sometimes we want to check for groups of characters using a Regular Expression and to achieve that we use parentheses `()`.
有时候我们想使用正则表达式里的括号 `()` 来检查字符组。
If you want to find either `Penguin` or `Pumpkin` in a string, you can use the following Regular Expression: `/P(engu|umpk)in/g`
如果想在字符串找到 `Penguin` `Pumpkin`,可以用这个正则表达式:`/P(engu|umpk)in/g`
Then check whether the desired string groups are in the test string by using the `test()` method.
然后使用 `test()` 方法检查 test 字符串里面是否包含字符组。
```js
let testStr = "Pumpkin";
let testRegex = /P(engu|umpk)in/;
testRegex.test(testStr);
// Returns true
```
`test` 方法会返回 `true`
# --instructions--
Fix the regex so that it checks for the names of `Franklin Roosevelt` or `Eleanor Roosevelt` in a case sensitive manner and it should make concessions for middle names.
完善正则表达式,使其以区分大小写的方式检查 `Franklin Roosevelt` `Eleanor Roosevelt` 的名字,并且应该忽略 middle names
Then fix the code so that the regex that you have created is checked against `myString` and either `true` or `false` is returned depending on whether the regex matches.
然后完善代码,使创建的正则检查 `myString`,根据正则是否匹配返回 `true` `false`
# --hints--
Your regex `myRegex` should return `true` for the string `Franklin D. Roosevelt`
正则 `myRegex` 测试 `Franklin D. Roosevelt` 应该返回 `true`
```js
myRegex.lastIndex = 0;
assert(myRegex.test('Franklin D. Roosevelt'));
```
Your regex `myRegex` should return `true` for the string `Eleanor Roosevelt`
正则 `myRegex` 测试 `Eleanor Roosevelt` 应该返回 `true`
```js
myRegex.lastIndex = 0;
assert(myRegex.test('Eleanor Roosevelt'));
```
Your regex `myRegex` should return `false` for the string `Franklin Rosevelt`
正则 `myRegex` 测试 `Franklin Rosevelt` 应该返回 `false`
```js
myRegex.lastIndex = 0;
assert(!myRegex.test('Franklin Rosevelt'));
```
Your regex `myRegex` should return `false` for the string `Frank Roosevelt`
正则 `myRegex` 测试 `Frank Roosevelt` 应该返回 `false`
```js
myRegex.lastIndex = 0;
assert(!myRegex.test('Frank Roosevelt'));
```
You should use `.test()` to test the regex.
你应该使用 `.test()` 方法来检测正则表达式。
```js
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
```
Your result should return `true`.
你的返回结果应该为 `true`
```js
assert(result === true);

View File

@ -1,6 +1,6 @@
---
id: 587d7db4367417b2b2512b92
title: Extract Matches
title: 提取匹配项
challengeType: 1
forumTopicId: 301340
dashedName: extract-matches
@ -8,22 +8,22 @@ dashedName: extract-matches
# --description--
So far, you have only been checking if a pattern exists or not within a string. You can also extract the actual matches you found with the `.match()` method.
到目前为止,只是检查了一个匹配模式是否存在于字符串中。 还可以使用 `.match()` 方法来提取找到的实际匹配项。
To use the `.match()` method, apply the method on a string and pass in the regex inside the parentheses.
可以使用字符串来调用 `.match()` 方法,并在括号内传入正则表达式。
Here's an example:
请看下面的举例:
```js
"Hello, World!".match(/Hello/);
// Returns ["Hello"]
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
// Returns ["expressions"]
```
Note that the `.match` syntax is the "opposite" of the `.test` method you have been using thus far:
这里第一个 `match` 将返回 `["Hello"]` 第二个将返回 `["expressions"]`
请注意, `.match` 语法是目前为止一直使用的 `.test` 方法中的“反向”:
```js
'string'.match(/regex/);
@ -32,23 +32,23 @@ Note that the `.match` syntax is the "opposite" of the `.test` method you have b
# --instructions--
Apply the `.match()` method to extract the word `coding`.
利用 `.match()` 方法提取单词 `coding`
# --hints--
The `result` should have the word `coding`
`result` 应该有字符串 `coding`
```js
assert(result.join() === 'coding');
```
Your regex `codingRegex` should search for `coding`
您的 regex `codingRegex` 应该搜索字符串 `coding`
```js
assert(codingRegex.source === 'coding');
```
You should use the `.match()` method.
您应该使用 `.match()` 方法。
```js
assert(code.match(/\.match\(.*\)/));

View File

@ -1,6 +1,6 @@
---
id: 587d7db6367417b2b2512b9b
title: Find Characters with Lazy Matching
title: 用惰性匹配来查找字符
challengeType: 1
forumTopicId: 301341
dashedName: find-characters-with-lazy-matching
@ -8,36 +8,35 @@ dashedName: find-characters-with-lazy-matching
# --description--
In regular expressions, a <dfn>greedy</dfn> match finds the longest possible part of a string that fits the regex pattern and returns it as a match. The alternative is called a <dfn>lazy</dfn> match, which finds the smallest possible part of the string that satisfies the regex pattern.
在正则表达式中,贪婪(<dfn>greedy</dfn>)匹配会匹配到符合正则表达式匹配模式的字符串的最长可能部分,并将其作为匹配项返回。 另一种方案称为懒惰(<dfn>lazy</dfn>)匹配,它会匹配到满足正则表达式的字符串的最小可能部分。
You can apply the regex `/t[a-z]*i/` to the string `"titanic"`. This regex is basically a pattern that starts with `t`, ends with `i`, and has some letters in between.
可以将正则表达式 `/t[a-z]*i/` 应用于字符串 `"titanic"`。 这个正则表达式是一个以 `t` 开始,以 `i` 结束,并且中间有一些字母的匹配模式。
Regular expressions are by default greedy, so the match would return `["titani"]`. It finds the largest sub-string possible to fit the pattern.
正则表达式默认是贪婪匹配,因此匹配返回为 `["titani"]`。 它会匹配到适合该匹配模式的最大子字符串。
However, you can use the `?` character to change it to lazy matching. `"titanic"` matched against the adjusted regex of `/t[a-z]*?i/` returns `["ti"]`.
但是,你可以使用 `?` 字符来将其变成懒惰匹配。 调整后的正则表达式 `/t[a-z]*?i/` 匹配字符串 `"titanic"` 返回 `["ti"]`
**Note**
Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.
**注意:**应该避免使用正则表达式解析 HTML但是可以用正则表达式匹配 HTML 字符串。
# --instructions--
Fix the regex `/<.*>/` to return the HTML tag `<h1>` and not the text `"<h1>Winter is coming</h1>"`. Remember the wildcard `.` in a regular expression matches any character.
修复正则表达式 `/<.*>/`,让它返回 HTML 标签 `<h1>`,而不是文本 `"<h1>Winter is coming</h1>"`。 请记得在正则表达式中使用通配符 `.` 来匹配任意字符。
# --hints--
The `result` variable should be an array with `<h1>` in it
`result` 变量应该是一个包含 `<h1>` 的数组
```js
assert(result[0] == '<h1>');
```
`myRegex` should use lazy matching
`myRegex` 应该使用懒惰匹配
```js
assert(/\?/g.test(myRegex));
```
`myRegex` should not include the string 'h1'
`myRegex` 不应包含字符串 `h1`
```js
assert(!myRegex.source.match('h1'));

View File

@ -1,6 +1,6 @@
---
id: 587d7db4367417b2b2512b93
title: Find More Than the First Match
title: 全局匹配
challengeType: 1
forumTopicId: 301342
dashedName: find-more-than-the-first-match
@ -8,45 +8,47 @@ dashedName: find-more-than-the-first-match
# --description--
So far, you have only been able to extract or search a pattern once.
到目前为止,只能提取或搜寻一次模式匹配。
```js
let testStr = "Repeat, Repeat, Repeat";
let ourRegex = /Repeat/;
testStr.match(ourRegex);
// Returns ["Repeat"]
```
To search or extract a pattern more than once, you can use the `g` flag.
在这里 `match` 将返回 `["Repeat"]`
若要多次搜寻或提取模式匹配,可以使用 `g` 标志。
```js
let repeatRegex = /Repeat/g;
testStr.match(repeatRegex);
// Returns ["Repeat", "Repeat", "Repeat"]
```
这里 `match` 返回值 `["Repeat", "Repeat", "Repeat"]`
# --instructions--
Using the regex `starRegex`, find and extract both `"Twinkle"` words from the string `twinkleStar`.
使用正则表达式 `starRegex`,从字符串 `twinkleStar` 中匹配所有的 `Twinkle` 单词并提取出来。
**Note**
You can have multiple flags on your regex like `/search/gi`
**注意:**
在正则表达式上可以有多个标志,比如 `/search/gi`
# --hints--
Your regex `starRegex` should use the global flag `g`
你的正则表达式 `starRegex` 应该使用全局标志 `g`
```js
assert(starRegex.flags.match(/g/).length == 1);
```
Your regex `starRegex` should use the case insensitive flag `i`
你的正则表达式 `starRegex` 应该使用忽略大小写标志 `i`
```js
assert(starRegex.flags.match(/i/).length == 1);
```
Your match should match both occurrences of the word `"Twinkle"`
你的匹配应该匹配单词 `Twinkle` 的两个匹配项
```js
assert(
@ -58,7 +60,7 @@ assert(
);
```
Your match `result` should have two elements in it.
你的匹配 `result` 应该包含两个元素
```js
assert(result.length == 2);

View File

@ -1,6 +1,6 @@
---
id: 587d7db7367417b2b2512b9c
title: Find One or More Criminals in a Hunt
title: 在狩猎中找到一个或多个罪犯
challengeType: 1
forumTopicId: 301343
dashedName: find-one-or-more-criminals-in-a-hunt
@ -8,11 +8,11 @@ dashedName: find-one-or-more-criminals-in-a-hunt
# --description--
Time to pause and test your new regex writing skills. A group of criminals escaped from jail and ran away, but you don't know how many. However, you do know that they stay close together when they are around other people. You are responsible for finding all of the criminals at once.
是时候停一停来测试你的正则表达式使用能力了。 一群罪犯越狱逃跑了,但你不知道有多少人。 然而,你知道他们不在一起时会保持紧密联系。 你有责任立刻找到所有的罪犯。
Here's an example to review how to do this:
这里有一个示例来提示如何做到这一点:
The regex `/z+/` matches the letter `z` when it appears one or more times in a row. It would find matches in all of the following strings:
当字母`z`在一行中出现一次或连续多次时,正则表达式`/z+/`会匹配到它。 它会在以下所有字符串中找到匹配项:
```js
"z"
@ -22,7 +22,7 @@ The regex `/z+/` matches the letter `z` when it appears one or more times in a r
"abczzzzzzzzzzzzzzzzzzzzzabc"
```
But it does not find matches in the following strings since there are no letter `z` characters:
但是它不会在以下字符串中找到匹配项,因为它们中没有字母`z`
```js
""
@ -32,32 +32,32 @@ But it does not find matches in the following strings since there are no letter
# --instructions--
Write a greedy regex that finds one or more criminals within a group of other people. A criminal is represented by the capital letter `C`.
编写一个贪婪正则表达式,在一组其他人中匹配到一个或多个罪犯。 罪犯由大写字母`C`表示。
# --hints--
Your regex should match one criminal (`C`) in `"C"`
您的正则表达式应该匹配字符串 `C` 中的一个罪犯(`C`)
```js
assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
```
Your regex should match two criminals (`CC`) in `"CC"`
您的正则表达式应该匹配字符串 `CC` 中的两个罪犯(`CC`)
```js
assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
```
Your regex should match three criminals (`CCC`) in `"P1P5P4CCCP2P6P3"`
您的正则表达式应该在字符串 `P1P5P4CCCcP2P6P3` 中匹配三个罪犯(`CCC`)。
```js
assert(
'P1P5P4CCCP2P6P3'.match(reCriminals) &&
'P1P5P4CCCP2P6P3'.match(reCriminals)[0] == 'CCC'
'P1P5P4CCCcP2P6P3'.match(reCriminals) &&
'P1P5P4CCCcP2P6P3'.match(reCriminals)[0] == 'CCC'
);
```
Your regex should match five criminals (`CCCCC`) in `"P6P2P7P4P5CCCCCP3P1"`
您的正则表达式应该在字符串 `P6P2P7P4P5CCCCCP3P1`中匹配五个罪犯(`CCCCC`)
```js
assert(
@ -66,19 +66,19 @@ assert(
);
```
Your regex should not match any criminals in `""`
你的正则表达式在`""`中不应该匹配到任何罪犯
```js
assert(!reCriminals.test(''));
```
Your regex should not match any criminals in `"P1P2P3"`
你的正则表达式在`P1P2P3`中不应该匹配到任何罪犯
```js
assert(!reCriminals.test('P1P2P3'));
```
Your regex should match fifty criminals (`CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC`) in `"P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3"`.
你的正则表达式应该在`P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3`中匹配五十个 罪犯('`CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC`')。
```js
assert(

View File

@ -1,6 +1,6 @@
---
id: 587d7db4367417b2b2512b91
title: Ignore Case While Matching
title: 匹配时忽略大小写
challengeType: 1
forumTopicId: 301344
dashedName: ignore-case-while-matching
@ -8,73 +8,73 @@ dashedName: ignore-case-while-matching
# --description--
Up until now, you've looked at regexes to do literal matches of strings. But sometimes, you might want to also match case differences.
到目前为止,已经了解了如何用正则表达式来执行字符串的匹配。 但有时候,并不关注匹配字母的大小写。
Case (or sometimes letter case) is the difference between uppercase letters and lowercase letters. Examples of uppercase are `"A"`, `"B"`, and `"C"`. Examples of lowercase are `"a"`, `"b"`, and `"c"`.
大小写即大写字母和小写字母。 大写字母如 `A``B``C`。 小写字母如 `a``b``c`
You can match both cases using what is called a flag. There are other flags but here you'll focus on the flag that ignores case - the `i` flag. You can use it by appending it to the regex. An example of using this flag is `/ignorecase/i`. This regex can match the strings `"ignorecase"`, `"igNoreCase"`, and `"IgnoreCase"`.
可以使用标志flag来匹配这两种情况。 标志有很多,不过这里我们只关注忽略大小写的标志——`i`。 可以通过将它附加到正则表达式之后来使用它。 这里给出使用该标志的一个实例 `/ignorecase/i`。 这个字符串可以匹配字符串 `ignorecase``igNoreCase` `IgnoreCase`
# --instructions--
Write a regex `fccRegex` to match `"freeCodeCamp"`, no matter its case. Your regex should not match any abbreviations or variations with spaces.
编写正则表达式 `fccRegex` 以匹配 `freeCodeCamp`,忽略大小写。 正则表达式不应与任何缩写或带有空格的变体匹配。
# --hints--
Your regex should match `freeCodeCamp`
你的正则表达式应该匹配 `freeCodeCamp`
```js
assert(fccRegex.test('freeCodeCamp'));
```
Your regex should match `FreeCodeCamp`
你的正则表达式应该匹配 `FreeCodeCamp`
```js
assert(fccRegex.test('FreeCodeCamp'));
```
Your regex should match `FreecodeCamp`
你的正则表达式应该匹配 `FreecodeCamp`
```js
assert(fccRegex.test('FreecodeCamp'));
```
Your regex should match `FreeCodecamp`
你的正则表达式应该匹配 `FreeCodecamp`
```js
assert(fccRegex.test('FreeCodecamp'));
```
Your regex should not match `Free Code Camp`
你的正则表达式不应该匹配 `Free Code Camp`
```js
assert(!fccRegex.test('Free Code Camp'));
```
Your regex should match `FreeCOdeCamp`
您的正则表达式应该匹配字符串 `FreeCOdeCamp`
```js
assert(fccRegex.test('FreeCOdeCamp'));
```
Your regex should not match `FCC`
你的正则表达式不应该匹配 `FCC`
```js
assert(!fccRegex.test('FCC'));
```
Your regex should match `FrEeCoDeCamp`
你的正则表达式应该匹配字符串 `FrEeCoDeCamp`
```js
assert(fccRegex.test('FrEeCoDeCamp'));
```
Your regex should match `FrEeCodECamp`
你的正则表达式应该匹配字符串 `FrEeCodECamp`
```js
assert(fccRegex.test('FrEeCodECamp'));
```
Your regex should match `FReeCodeCAmp`
你的正则表达式应该匹配字符串 `FReeCodeCAmp`
```js
assert(fccRegex.test('FReeCodeCAmp'));

View File

@ -1,6 +1,6 @@
---
id: 587d7db4367417b2b2512b90
title: Match a Literal String with Different Possibilities
title: 同时用多种模式匹配文字字符串
challengeType: 1
forumTopicId: 301345
dashedName: match-a-literal-string-with-different-possibilities
@ -8,57 +8,57 @@ dashedName: match-a-literal-string-with-different-possibilities
# --description--
Using regexes like `/coding/`, you can look for the pattern `"coding"` in another string.
使用正则表达式`/coding/`,你可以在其他字符串中查找`coding`
This is powerful to search single strings, but it's limited to only one pattern. You can search for multiple patterns using the `alternation` or `OR` operator: `|`.
这对于搜寻单个字符串非常有用,但仅限于一种匹配模式。 你可以使用 `alternation` `OR` 操作符搜索多个模式: `|`
This operator matches patterns either before or after it. For example, if you wanted to match `"yes"` or `"no"`, the regex you want is `/yes|no/`.
此操作符匹配操作符前面或后面的字符。 例如,如果你想匹配 `yes` `no`,你需要的正则表达式是 `/yes|no/`
You can also search for more than just two patterns. You can do this by adding more patterns with more `OR` operators separating them, like `/yes|no|maybe/`.
你还可以匹配多个规则,这可以通过添加更多的匹配模式来实现。 这些匹配模式将包含更多的 `OR` 操作符来分隔它们,比如`/yes|no|maybe/`
# --instructions--
Complete the regex `petRegex` to match the pets `"dog"`, `"cat"`, `"bird"`, or `"fish"`.
完成正则表达式 `petRegex` 以匹配 `dog``cat``bird` 或者 `fish`
# --hints--
Your regex `petRegex` should return `true` for the string `"John has a pet dog."`
对于字符串 `John has a pet dog.`,你的正则表达式`petRegex` 应该返回 `true`
```js
assert(petRegex.test('John has a pet dog.'));
```
Your regex `petRegex` should return `false` for the string `"Emma has a pet rock."`
对于字符串 `Emma has a pet rock.`,你的正则表达式 `petRegex` 的应该返回 `false`
```js
assert(!petRegex.test('Emma has a pet rock.'));
```
Your regex `petRegex` should return `true` for the string `"Emma has a pet bird."`
对于字符串 `Emma has a pet bird.`,你的正则表达式 `petRegex` 应该返回 `true`
```js
assert(petRegex.test('Emma has a pet bird.'));
```
Your regex `petRegex` should return `true` for the string `"Liz has a pet cat."`
对于字符串 `Liz has a pet cat.`,你的正则表达式 `petRegex` 应该返回 `true`
```js
assert(petRegex.test('Liz has a pet cat.'));
```
Your regex `petRegex` should return `false` for the string `"Kara has a pet dolphin."`
对于字符串 `Kara has a pet dolphin.`,你的正则表达式 `petRegex` 应该返回 `false`
```js
assert(!petRegex.test('Kara has a pet dolphin.'));
```
Your regex `petRegex` should return `true` for the string `"Alice has a pet fish."`
对于字符串 `Alice has a pet fish.`,你的正则表达式 `petRegex` 应该返回 `true`
```js
assert(petRegex.test('Alice has a pet fish.'));
```
Your regex `petRegex` should return `false` for the string `"Jimmy has a pet computer."`
对于字符串 `Jimmy has a pet computer.`,你的正则表达式 `petRegex` 应该返回 `false`
```js
assert(!petRegex.test('Jimmy has a pet computer.'));

View File

@ -1,6 +1,6 @@
---
id: 587d7db7367417b2b2512b9f
title: Match All Letters and Numbers
title: 匹配所有的字母和数字
challengeType: 1
forumTopicId: 301346
dashedName: match-all-letters-and-numbers
@ -8,42 +8,44 @@ dashedName: match-all-letters-and-numbers
# --description--
Using character classes, you were able to search for all letters of the alphabet with `[a-z]`. This kind of character class is common enough that there is a shortcut for it, although it includes a few extra characters as well.
使用元字符,可以使用 `[a-z]` 搜寻字母表中的所有字母。 这种元字符是很常见的,它有一个缩写,但这个缩写也包含额外的字符。
The closest character class in JavaScript to match the alphabet is `\w`. This shortcut is equal to `[A-Za-z0-9_]`. This character class matches upper and lowercase letters plus numbers. Note, this character class also includes the underscore character (`_`).
JavaScript 中与字母表匹配的最接近的元字符是`\w`。 这个缩写等同于`[A-Za-z0-9_]`。 此字符类匹配上面字母和小写字母以及数字。 注意,这个字符类也包含下划线字符 (`_`)
```js
let longHand = /[A-Za-z0-9_]+/;
let shortHand = /\w+/;
let numbers = "42";
let varNames = "important_var";
longHand.test(numbers); // Returns true
shortHand.test(numbers); // Returns true
longHand.test(varNames); // Returns true
shortHand.test(varNames); // Returns true
longHand.test(numbers);
shortHand.test(numbers);
longHand.test(varNames);
shortHand.test(varNames);
```
These shortcut character classes are also known as <dfn>shorthand character classes</dfn>.
上面的 `test` 都会返回 `true`
这些元字符缩写也被称为短语元字符 <dfn>shorthand character classes</dfn>
# --instructions--
Use the shorthand character class `\w` to count the number of alphanumeric characters in various quotes and strings.
使用元字符 `\w` 来计算所有引号中字母和数字字符的数量。
# --hints--
Your regex should use the global flag.
您的正则表达式应该使用全局标识。
```js
assert(alphabetRegexV2.global);
```
Your regex should use the shorthand character `\w` to match all characters which are alphanumeric.
正则表达式应该使用元字符r `\w` 来匹配非字母字符。
```js
assert(/\\w/.test(alphabetRegexV2.source));
```
Your regex should find 31 alphanumeric characters in `"The five boxing wizards jump quickly."`
你的正则表达式应该在 `The five boxing wizards jump quickly.` 中匹配到 31 个非字母数字字符。
```js
assert(
@ -51,7 +53,7 @@ assert(
);
```
Your regex should find 32 alphanumeric characters in `"Pack my box with five dozen liquor jugs."`
你的正则表达式应该在 `Pack my box with five dozen liquor jugs.` 中匹配到 32 个非字母数字字符。
```js
assert(
@ -60,7 +62,7 @@ assert(
);
```
Your regex should find 30 alphanumeric characters in `"How vexingly quick daft zebras jump!"`
你的正则表达式应该在 `How vexingly quick daft zebras jump!` 中匹配到 30 个非字母数字字符。
```js
assert(
@ -68,7 +70,7 @@ assert(
);
```
Your regex should find 36 alphanumeric characters in `"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."`
你的正则表达式应该在字符串 `123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.` 中找到36个字母数字字符。
```js
assert(

View File

@ -1,6 +1,6 @@
---
id: 587d7db8367417b2b2512ba1
title: Match All Non-Numbers
title: 匹配所有非数字
challengeType: 1
forumTopicId: 301347
dashedName: match-all-non-numbers
@ -8,59 +8,59 @@ dashedName: match-all-non-numbers
# --description--
The last challenge showed how to search for digits using the shortcut `\d` with a lowercase `d`. You can also search for non-digits using a similar shortcut that uses an uppercase `D` instead.
上一项挑战中展示了如何使用带有小写 `d` 的缩写 `\d` 来搜寻数字。 也可以使用类似的缩写来搜寻非数字,该缩写使用大写的 `D`
The shortcut to look for non-digit characters is `\D`. This is equal to the character class `[^0-9]`, which looks for a single character that is not a number between zero and nine.
查找非数字字符的缩写是 `\D`。 这等同于字符串 `[^0-9]`,它查找不是 0 - 9 之间数字的单个字符。
# --instructions--
Use the shorthand character class for non-digits `\D` to count how many non-digits are in movie titles.
使用非数字缩写 `\D` 来计算电影标题中有多少非数字。
# --hints--
Your regex should use the shortcut character to match non-digit characters
你的正则表达式应该使用缩写来匹配非数字字符。
```js
assert(/\\D/.test(noNumRegex.source));
```
Your regex should use the global flag.
你的正则表达式应该使用全局标识。
```js
assert(noNumRegex.global);
```
Your regex should find no non-digits in `"9"`.
你的正则表达式在 `9` 中应该匹配不到非数字。
```js
assert('9'.match(noNumRegex) == null);
```
Your regex should find 6 non-digits in `"Catch 22"`.
你的正则表达式应该在 `Catch 22` 中匹配到 6 个非数字。
```js
assert('Catch 22'.match(noNumRegex).length == 6);
```
Your regex should find 11 non-digits in `"101 Dalmatians"`.
你的正则表达式应该在 `101 Dalmatians` 中匹配到 11 个非数字。
```js
assert('101 Dalmatians'.match(noNumRegex).length == 11);
```
Your regex should find 15 non-digits in `"One, Two, Three"`.
你的正则表达式应该在 `One, Two, Three` 中匹配到 15 个非数字。
```js
assert('One, Two, Three'.match(noNumRegex).length == 15);
```
Your regex should find 12 non-digits in `"21 Jump Street"`.
你的正则表达式应该在 `21 Jump Street` 中匹配到 12 个非数字。
```js
assert('21 Jump Street'.match(noNumRegex).length == 12);
```
Your regex should find 17 non-digits in `"2001: A Space Odyssey"`.
你的正则表达式应该在 `2001: A Space Odyssey` 中匹配到 17 个非数字。
```js
assert('2001: A Space Odyssey'.match(noNumRegex).length == 17);

View File

@ -1,6 +1,6 @@
---
id: 5d712346c441eddfaeb5bdef
title: Match All Numbers
title: 匹配所有数字
challengeType: 1
forumTopicId: 18181
dashedName: match-all-numbers
@ -8,59 +8,59 @@ dashedName: match-all-numbers
# --description--
You've learned shortcuts for common string patterns like alphanumerics. Another common pattern is looking for just digits or numbers.
已经了解了常见字符串匹配模式的元字符,如字母数字。 另一个常见的匹配模式是只寻找数字。
The shortcut to look for digit characters is `\d`, with a lowercase `d`. This is equal to the character class `[0-9]`, which looks for a single character of any number between zero and nine.
查找数字字符的缩写是 `\d`,注意是小写的 `d`。 这等同于元字符 `[0-9]`,它查找 0 到 9 之间任意数字的单个字符。
# --instructions--
Use the shorthand character class `\d` to count how many digits are in movie titles. Written out numbers ("six" instead of 6) do not count.
使用缩写 `\d` 来计算电影标题中有多少个数字。 书面数字("six" 而不是 6不计算在内。
# --hints--
Your regex should use the shortcut character to match digit characters
你的正则表达式应该使用缩写来匹配数字字符。
```js
assert(/\\d/.test(numRegex.source));
```
Your regex should use the global flag.
您的正则表达式应该使用全局标识。
```js
assert(numRegex.global);
```
Your regex should find 1 digit in `"9"`.
你的正则表达式应该在 `9` 中匹配到 1 个数字。
```js
assert('9'.match(numRegex).length == 1);
```
Your regex should find 2 digits in `"Catch 22"`.
你的正则表达式应该在 `Catch 22` 中匹配到 2 个数字。
```js
assert('Catch 22'.match(numRegex).length == 2);
```
Your regex should find 3 digits in `"101 Dalmatians"`.
你的正则表达式应该在 `101 Dalmatians` 中匹配到 3 个数字。
```js
assert('101 Dalmatians'.match(numRegex).length == 3);
```
Your regex should find no digits in `"One, Two, Three"`.
你的正则表达式在 `One, Two, Three` 中应该匹配不到数字。
```js
assert('One, Two, Three'.match(numRegex) == null);
```
Your regex should find 2 digits in `"21 Jump Street"`.
你的正则表达式应该在 `21 Jump Street` 中匹配到 2 个数字。
```js
assert('21 Jump Street'.match(numRegex).length == 2);
```
Your regex should find 4 digits in `"2001: A Space Odyssey"`.
你的正则表达式应该在 `2001: A Space Odyssey` 中匹配到 4 个数字。
```js
assert('2001: A Space Odyssey'.match(numRegex).length == 4);

View File

@ -1,6 +1,6 @@
---
id: 587d7db5367417b2b2512b94
title: Match Anything with Wildcard Period
title: 用通配符匹配任何内容
challengeType: 1
forumTopicId: 301348
dashedName: match-anything-with-wildcard-period
@ -8,72 +8,74 @@ dashedName: match-anything-with-wildcard-period
# --description--
Sometimes you won't (or don't need to) know the exact characters in your patterns. Thinking of all words that match, say, a misspelling would take a long time. Luckily, you can save time using the wildcard character: `.`
有时不(或不需要)知道匹配模式中的确切字符。 如果要精确匹配到完整的单词,那出现一个拼写错误就会匹配不到。 幸运的是,可以使用通配符 `.` 来处理这种情况。
The wildcard character `.` will match any one character. The wildcard is also called `dot` and `period`. You can use the wildcard character just like any other character in the regex. For example, if you wanted to match `"hug"`, `"huh"`, `"hut"`, and `"hum"`, you can use the regex `/hu./` to match all four words.
通配符 `.` 将匹配任何一个字符。 通配符也叫 `dot` `period`。 可以像使用正则表达式中任何其他字符一样使用通配符。 例如,如果想匹配 `hug``huh``hut` `hum`,可以使用正则表达式 `/hu./` 匹配以上四个单词。
```js
let humStr = "I'll hum a song";
let hugStr = "Bear hug";
let huRegex = /hu./;
huRegex.test(humStr); // Returns true
huRegex.test(hugStr); // Returns true
huRegex.test(humStr);
huRegex.test(hugStr);
```
上面的 `test` 都会返回 `true`
# --instructions--
Complete the regex `unRegex` so that it matches the strings `"run"`, `"sun"`, `"fun"`, `"pun"`, `"nun"`, and `"bun"`. Your regex should use the wildcard character.
完成正则表达式 `unRegex` 以匹配字符串 `run``sun``fun``pun``nun` `bun`。 正则表达式中应该使用通配符。
# --hints--
You should use the `.test()` method.
你应该使用 `.test()` 方法。
```js
assert(code.match(/\.test\(.*\)/));
```
You should use the wildcard character in your regex `unRegex`
你应该在你的正则表达式 `unRegex` 中使用通配符。
```js
assert(/\./.test(unRegex.source));
```
Your regex `unRegex` should match `"run"` in `"Let us go on a run."`
你的正则表达式 `unRegex` 应该在字符串 `Let us go on a run.` 中匹配到 `run` 单词。
```js
unRegex.lastIndex = 0;
assert(unRegex.test('Let us go on a run.'));
```
Your regex `unRegex` should match `"sun"` in `"The sun is out today."`
你的正则表达式 `unRegex` 应该在字符串 `The sun is out today.` 中匹配到 `sun` 单词。
```js
unRegex.lastIndex = 0;
assert(unRegex.test('The sun is out today.'));
```
Your regex `unRegex` should match `"fun"` in `"Coding is a lot of fun."`
你的正则表达式 `unRegex` 应该在字符串 `Coding is a lot of fun.` 中匹配到 `fun` 单词。
```js
unRegex.lastIndex = 0;
assert(unRegex.test('Coding is a lot of fun.'));
```
Your regex `unRegex` should match `"pun"` in `"Seven days without a pun makes one weak."`
你的正则表达式 `unRegex` 应该在字符串 `Seven days without a pun makes one weak.` 中匹配到 `pun`单词。
```js
unRegex.lastIndex = 0;
assert(unRegex.test('Seven days without a pun makes one weak.'));
```
Your regex `unRegex` should match `"nun"` in `"One takes a vow to be a nun."`
你的正则表达式 `unRegex` 应该在字符串 `One takes a vow to be a nun.` 中匹配到 `nun` 单词。
```js
unRegex.lastIndex = 0;
assert(unRegex.test('One takes a vow to be a nun.'));
```
Your regex `unRegex` should match `"bun"` in `"She got fired from the hot dog stand for putting her hair in a bun."`
你的正则表达式 `unRegex` 应该在字符串 `She got fired from the hot dog stand for putting her hair in a bun.` 中匹配到 `bun` 单词。
```js
unRegex.lastIndex = 0;
@ -84,14 +86,14 @@ assert(
);
```
Your regex `unRegex` should not match `"There is a bug in my code."`
你的正则表达式 `unRegex` 不应该匹配 `There is a bug in my code.`
```js
unRegex.lastIndex = 0;
assert(!unRegex.test('There is a bug in my code.'));
```
Your regex `unRegex` should not match `"Catch me if you can."`
你的正则表达式 `unRegex` 不应该匹配 `Catch me if you can.`
```js
unRegex.lastIndex = 0;

View File

@ -1,6 +1,6 @@
---
id: 587d7db7367417b2b2512b9d
title: Match Beginning String Patterns
title: 匹配字符串的开头
challengeType: 1
forumTopicId: 301349
dashedName: match-beginning-string-patterns
@ -8,45 +8,45 @@ dashedName: match-beginning-string-patterns
# --description--
Prior challenges showed that regular expressions can be used to look for a number of matches. They are also used to search for patterns in specific positions in strings.
回顾一下之前的挑战,正则表达式可以用于查找多项匹配。 还可以查询字符串中符合指定匹配模式的字符。
In an earlier challenge, you used the caret character (`^`) inside a character set to create a negated character set in the form `[^thingsThatWillNotBeMatched]`. Outside of a character set, the caret is used to search for patterns at the beginning of strings.
在之前的挑战中,使用字符集中前插入符号(`^`)来创建一个否定字符集,形如 `[^thingsThatWillNotBeMatched]`。 除了在字符集中使用之外,脱字符还用于匹配字符串的开始位置。
```js
let firstString = "Ricky is first and can be found.";
let firstRegex = /^Ricky/;
firstRegex.test(firstString);
// Returns true
let notFirst = "You can't find Ricky now.";
firstRegex.test(notFirst);
// Returns false
```
第一次 `test` 调用将返回 `true`,而第二次调用将返回 `false`
# --instructions--
Use the caret character in a regex to find `"Cal"` only in the beginning of the string `rickyAndCal`.
在正则表达式中使用脱字符来找到 `Cal` 在字符串 `rickyAndCal` 中的开始位置。
# --hints--
Your regex should search for `"Cal"` with a capital letter.
你的正则表达式应该搜寻首字母大写的 `Cal`
```js
assert(calRegex.source == '^Cal');
```
Your regex should not use any flags.
你的正则表达式不应该使用任何标志。
```js
assert(calRegex.flags == '');
```
Your regex should match `"Cal"` at the beginning of the string.
你的正则表达式应该匹配字符串 `Cal` 的开始位置。
```js
assert(calRegex.test('Cal and Ricky both like racing.'));
```
Your regex should not match `"Cal"` in the middle of a string.
你的正则表达式不应该匹配中间包含 `Cal` 的字符串。
```js
assert(!calRegex.test('Ricky and Cal both like racing.'));

View File

@ -1,6 +1,6 @@
---
id: 587d7db6367417b2b2512b99
title: Match Characters that Occur One or More Times
title: 匹配出现一次或多次的字符
challengeType: 1
forumTopicId: 301350
dashedName: match-characters-that-occur-one-or-more-times
@ -8,33 +8,33 @@ dashedName: match-characters-that-occur-one-or-more-times
# --description--
Sometimes, you need to match a character (or group of characters) that appears one or more times in a row. This means it occurs at least once, and may be repeated.
有时,需要匹配出现一次或者连续多次的的字符(或字符组)。 这意味着它至少出现一次,并且可能重复出现。
You can use the `+` character to check if that is the case. Remember, the character or pattern has to be present consecutively. That is, the character has to repeat one after the other.
可以使用 `+` 符号来检查情况是否如此。 记住,字符或匹配模式必须一个接一个地连续出现。 这就是说,字符必须一个接一个地重复。
For example, `/a+/g` would find one match in `"abc"` and return `["a"]`. Because of the `+`, it would also find a single match in `"aabc"` and return `["aa"]`.
例如,`/a+/g` 会在 `abc` 中匹配到一个匹配项,并且返回 `["a"]`。 因为 `+` 的存在,它也会在 `aabc` 中匹配到一个匹配项,然后返回 `["aa"]`
If it were instead checking the string `"abab"`, it would find two matches and return `["a", "a"]` because the `a` characters are not in a row - there is a `b` between them. Finally, since there is no `"a"` in the string `"bcd"`, it wouldn't find a match.
如果它是检查字符串 `abab`,它将匹配到两个匹配项并且返回`["a", "a"]`,因为`a`字符不连续,在它们之间有一个`b`字符。 最后,因为在字符串 `bcd` 中没有 `a`,因此找不到匹配项。
# --instructions--
You want to find matches when the letter `s` occurs one or more times in `"Mississippi"`. Write a regex that uses the `+` sign.
想要在字符串 `Mississippi` 中匹配到出现一次或多次的字母 `s` 的匹配项。 编写一个使用 `+` 符号的正则表达式。
# --hints--
Your regex `myRegex` should use the `+` sign to match one or more `s` characters.
你的正则表达式 `myRegex` 应该使用 `+` 符号来匹配一个或多个 `s` 字符。
```js
assert(/\+/.test(myRegex.source));
```
Your regex `myRegex` should match 2 items.
你的正则表达式 `myRegex` 应该匹配两项。
```js
assert(result.length == 2);
```
The `result` variable should be an array with two matches of `"ss"`
`result` 变量应该是一个数组,两个匹配的 `ss`
```js
assert(result[0] == 'ss' && result[1] == 'ss');

View File

@ -1,6 +1,6 @@
---
id: 587d7db6367417b2b2512b9a
title: Match Characters that Occur Zero or More Times
title: 匹配出现零次或多次的字符
challengeType: 1
forumTopicId: 301351
dashedName: match-characters-that-occur-zero-or-more-times
@ -8,51 +8,53 @@ dashedName: match-characters-that-occur-zero-or-more-times
# --description--
The last challenge used the plus `+` sign to look for characters that occur one or more times. There's also an option that matches characters that occur zero or more times.
上一次的挑战中使用了加号 `+` 来查找出现一次或多次的字符。 还有一个选项可以匹配出现零次或多次的字符。
The character to do this is the asterisk or star: `*`.
执行该操作的字符叫做星号,即`*`
```js
let soccerWord = "gooooooooal!";
let gPhrase = "gut feeling";
let oPhrase = "over the moon";
let goRegex = /go*/;
soccerWord.match(goRegex); // Returns ["goooooooo"]
gPhrase.match(goRegex); // Returns ["g"]
oPhrase.match(goRegex); // Returns null
soccerWord.match(goRegex);
gPhrase.match(goRegex);
oPhrase.match(goRegex);
```
按顺序排列,三次 `match` 调用将返回值 `["goooooooo"]``["g"]``null`
# --instructions--
For this challenge, `chewieQuote` has been initialized as "Aaaaaaaaaaaaaaaarrrgh!" behind the scenes. Create a regex `chewieRegex` that uses the `*` character to match an uppercase `"A"` character immediately followed by zero or more lowercase `"a"` characters in `chewieQuote`. Your regex does not need flags or character classes, and it should not match any of the other quotes.
在这个挑战里,`chewieQuote` 已经被初始化为 `Aaaaaaaaaaaaaaaarrrgh!`。 创建一个变量为 `chewieRegex` 的正则表达式,使用 `*``chewieQuote` 中匹配 `A` 及其之后出现的零个或多个`a`。 你的正则表达式不需要使用修饰符,也不需要匹配引号。
# --hints--
Your regex `chewieRegex` should use the `*` character to match zero or more `a` characters.
你的正则表达式 `chewieRegex` 应该使用 `*` 字符来匹配零或更多的 `a` 字符。
```js
assert(/\*/.test(chewieRegex.source));
```
Your regex should match `"A"` in `chewieQuote`.
正则表达式应当匹配 `chewieQuote` 里的 `A`
```js
assert(result[0][0] === 'A');
```
Your regex should match `"Aaaaaaaaaaaaaaaa"` in `chewieQuote`.
你的正则表达式应该与 `chewieQuote` 中的字符串 `Aaaaaaaaaaaaaaaa` 匹配。
```js
assert(result[0] === 'Aaaaaaaaaaaaaaaa');
```
Your regex `chewieRegex` should match 16 characters in `chewieQuote`.
你的正则表达式 `chewieRegex` 应该匹配 `chewieQuote` 中的 16 个字符。
```js
assert(result[0].length === 16);
```
Your regex should not match any characters in "He made a fair move. Screaming about it can't help you."
你的正则表达式不应该有任何匹配,在字符 `He made a fair move. Screaming about it can't help you.`
```js
assert(
@ -60,7 +62,7 @@ assert(
);
```
Your regex should not match any characters in "Let him have it. It's not wise to upset a Wookiee."
你的正则表达式不应该有任何匹配,在字符 `Let him have it. It's not wise to upset a Wookiee.`
```js
assert(

View File

@ -1,6 +1,6 @@
---
id: 587d7db7367417b2b2512b9e
title: Match Ending String Patterns
title: 匹配字符串的末尾
challengeType: 1
forumTopicId: 301352
dashedName: match-ending-string-patterns
@ -8,40 +8,39 @@ dashedName: match-ending-string-patterns
# --description--
In the last challenge, you learned to use the caret character to search for patterns at the beginning of strings. There is also a way to search for patterns at the end of strings.
在上一个挑战中,学习了使用脱字符号来搜寻字符串的开始位置。 还有一种方法可以搜寻字符串末尾的匹配模式。
You can search the end of strings using the dollar sign character `$` at the end of the regex.
可以使用正则表达式的美元符号 `$` 来搜寻字符串的结尾。
```js
let theEnding = "This is a never ending story";
let storyRegex = /story$/;
storyRegex.test(theEnding);
// Returns true
let noEnding = "Sometimes a story will have to end";
storyRegex.test(noEnding);
// Returns false
```
第一次 `test` 调用将返回 `true`, 而第二次调用将返回 `false`
# --instructions--
Use the anchor character (`$`) to match the string `"caboose"` at the end of the string `caboose`.
使用锚点字符 `$` 来匹配字符串 `caboose` 在字符串末尾 `caboose`
# --hints--
You should search for `"caboose"` with the dollar sign `$` anchor in your regex.
你应该在正则表达式使用美元符号 `$` 来搜寻 `caboose`
```js
assert(lastRegex.source == 'caboose$');
```
Your regex should not use any flags.
你的正则表达式不应该使用任何标志。
```js
assert(lastRegex.flags == '');
```
You should match `"caboose"` at the end of the string `"The last car on a train is the caboose"`
你应该在字符串 `The last car on a train is the caboose` 的末尾匹配 `caboose`
```js
assert(lastRegex.test('The last car on a train is the caboose'));

View File

@ -1,6 +1,6 @@
---
id: 587d7db8367417b2b2512ba0
title: Match Everything But Letters and Numbers
title: 匹配除了字母和数字的所有符号
challengeType: 1
forumTopicId: 301353
dashedName: match-everything-but-letters-and-numbers
@ -8,31 +8,33 @@ dashedName: match-everything-but-letters-and-numbers
# --description--
You've learned that you can use a shortcut to match alphanumerics `[A-Za-z0-9_]` using `\w`. A natural pattern you might want to search for is the opposite of alphanumerics.
已经了解到可以使用缩写 `\w` 来匹配字母和数字 `[A-Za-z0-9_]`。 不过,有可能想要搜寻的匹配模式是非字母数字字符。
You can search for the opposite of the `\w` with `\W`. Note, the opposite pattern uses a capital letter. This shortcut is the same as `[^A-Za-z0-9_]`.
可以使用 `\W` 搜寻和 `\w` 相反的匹配模式。 注意,相反匹配模式使用大写字母。 此缩写与 `[^A-Za-z0-9_]` 是一样的。
```js
let shortHand = /\W/;
let numbers = "42%";
let sentence = "Coding!";
numbers.match(shortHand); // Returns ["%"]
sentence.match(shortHand); // Returns ["!"]
numbers.match(shortHand);
sentence.match(shortHand);
```
第一次 `match` 调用将返回值 `["%"]` 而第二次调用将返回 `["!"]`
# --instructions--
Use the shorthand character class `\W` to count the number of non-alphanumeric characters in various quotes and strings.
使用缩写 `\W` 来计算引号中所有非字符字母和数字字符的数量。
# --hints--
Your regex should use the global flag.
你的正则表达式应该使用全局标识。
```js
assert(nonAlphabetRegex.global);
```
Your regex should find 6 non-alphanumeric characters in `"The five boxing wizards jump quickly."`.
你的正则表达式应该在 `The five boxing wizards jump quickly.` 中匹配到 6 个非字母数字字符。
```js
assert(
@ -40,13 +42,13 @@ assert(
);
```
Your regex should use the shorthand character to match characters which are non-alphanumeric.
正则表达式应该使用元字符来匹配非字母字符。
```js
assert(/\\W/.test(nonAlphabetRegex.source));
```
Your regex should find 8 non-alphanumeric characters in `"Pack my box with five dozen liquor jugs."`
你的正则表达式应该在 `Pack my box with five dozen liquor jugs.` 中匹配到 8 个非字母数字字符。
```js
assert(
@ -54,7 +56,7 @@ assert(
);
```
Your regex should find 6 non-alphanumeric characters in `"How vexingly quick daft zebras jump!"`
你的正则表达式应该在 `How vexingly quick daft zebras jump!` 中匹配到 6 个非字母数字字符。
```js
assert(
@ -62,7 +64,7 @@ assert(
);
```
Your regex should find 12 non-alphanumeric characters in `"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."`
你的正则表达式应该在字符串 `123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.` 中找到12个非字母数字字符。
```js
assert(

View File

@ -1,6 +1,6 @@
---
id: 587d7db5367417b2b2512b96
title: Match Letters of the Alphabet
title: 匹配字母表中的字母
challengeType: 1
forumTopicId: 301354
dashedName: match-letters-of-the-alphabet
@ -8,43 +8,45 @@ dashedName: match-letters-of-the-alphabet
# --description--
You saw how you can use <dfn>character sets</dfn> to specify a group of characters to match, but that's a lot of typing when you need to match a large range of characters (for example, every letter in the alphabet). Fortunately, there is a built-in feature that makes this short and simple.
了解了如何使用字符集(<dfn>character sets</dfn>)来指定要匹配的一组字符串,但是有时需要匹配大量字符(例如,字母表中的每个字母)。 有一种写法可以让实现这个功能变得简短。
Inside a character set, you can define a range of characters to match using a hyphen character: `-`.
在字符集中,可以使用连字符(`-`)来定义要匹配的字符范围。
For example, to match lowercase letters `a` through `e` you would use `[a-e]`.
例如,要匹配小写字母 `a``e`,你可以使用 `[a-e]`
```js
let catStr = "cat";
let batStr = "bat";
let matStr = "mat";
let bgRegex = /[a-e]at/;
catStr.match(bgRegex); // Returns ["cat"]
batStr.match(bgRegex); // Returns ["bat"]
matStr.match(bgRegex); // Returns null
catStr.match(bgRegex);
batStr.match(bgRegex);
matStr.match(bgRegex);
```
按顺序排列,三次 `match` 调用将返回值 `["cat"]``["bat"]``null`
# --instructions--
Match all the letters in the string `quoteSample`.
匹配字符串 `quoteSample` 中的所有字母。
**Note**: Be sure to match both uppercase and lowercase letters.
**注意**:一定要同时匹配大小写字母。
# --hints--
Your regex `alphabetRegex` should match 35 items.
你的正则表达式 `alphabetRegex` 应该匹配 35 项。
```js
assert(result.length == 35);
```
Your regex `alphabetRegex` should use the global flag.
你的正则表达式 `alphabetRegex` 应该使用全局标识。
```js
assert(alphabetRegex.flags.match(/g/).length == 1);
```
Your regex `alphabetRegex` should use the case insensitive flag.
你的正则表达式 `alphabetRegex` 应该使用忽略大小写标志。
```js
assert(alphabetRegex.flags.match(/i/).length == 1);

View File

@ -1,6 +1,6 @@
---
id: 587d7db3367417b2b2512b8f
title: Match Literal Strings
title: 匹配文字字符串
challengeType: 1
forumTopicId: 301355
dashedName: match-literal-strings
@ -8,44 +8,46 @@ dashedName: match-literal-strings
# --description--
In the last challenge, you searched for the word `"Hello"` using the regular expression `/Hello/`. That regex searched for a literal match of the string `"Hello"`. Here's another example searching for a literal match of the string `"Kevin"`:
在上一个挑战中,使用正则表达式 `/Hello/` 搜索到了字符串 `Hello`。 那个正则表达式在字符串中搜寻 `Hello` 的文字匹配。 下面是另一个在字符串中搜寻 `Kevin` 的示例:
```js
let testStr = "Hello, my name is Kevin.";
let testRegex = /Kevin/;
testRegex.test(testStr);
// Returns true
```
Any other forms of `"Kevin"` will not match. For example, the regex `/Kevin/` will not match `"kevin"` or `"KEVIN"`.
`test` 方法会返回 `true`
任何其他形式的 `Kevin` 都不会被匹配。 例如,正则表达式 `/Kevin/` 不会匹配 `kevin` 或者`KEVIN`
```js
let wrongRegex = /kevin/;
wrongRegex.test(testStr);
// Returns false
```
A future challenge will show how to match those other forms as well.
`test` 调用将返回 `false`
后续的挑战将为你展示如何匹配其他形式的字符串。
# --instructions--
Complete the regex `waldoRegex` to find `"Waldo"` in the string `waldoIsHiding` with a literal match.
完成正则表达式 `waldoRegex`,在字符串 `waldoIsHiding` 中匹配到文本 `"Waldo"`
# --hints--
Your regex `waldoRegex` should find `"Waldo"`
你的正则表达式 `waldoRegex` 应该匹配到 `Waldo`
```js
assert(waldoRegex.test(waldoIsHiding));
```
Your regex `waldoRegex` should not search for anything else.
你的正则表达式 `waldoRegex` 不应该搜寻其他的任何内容。
```js
assert(!waldoRegex.test('Somewhere is hiding in this text.'));
```
You should perform a literal string match with your regex.
你应该使用你的正则表达式对字符串执行文字匹配。
```js
assert(!/\/.*\/i/.test(code));

View File

@ -1,6 +1,6 @@
---
id: 587d7db9367417b2b2512ba4
title: Match Non-Whitespace Characters
title: 匹配非空白字符
challengeType: 1
forumTopicId: 18210
dashedName: match-non-whitespace-characters
@ -8,35 +8,37 @@ dashedName: match-non-whitespace-characters
# --description--
You learned about searching for whitespace using `\s`, with a lowercase `s`. You can also search for everything except whitespace.
已经学会了如何使用带有小写 `s` 的缩写 `\s` 来搜寻空白字符。 还可以搜寻除了空格之外的所有内容。
Search for non-whitespace using `\S`, which is an uppercase `s`. This pattern will not match whitespace, carriage return, tab, form feed, and new line characters. You can think of it being similar to the character class `[^ \r\t\f\n\v]`.
使用 `\S` 搜寻非空白字符,其中 `s` 是大写。 此匹配模式将不匹配空格、回车符、制表符、换页符和换行符。 可以认为这类似于元字符 `[^ \r\t\f\n\v]`
```js
let whiteSpace = "Whitespace. Whitespace everywhere!"
let nonSpaceRegex = /\S/g;
whiteSpace.match(nonSpaceRegex).length; // Returns 32
whiteSpace.match(nonSpaceRegex).length;
```
返回值的 `.length` 应该是 `32`
# --instructions--
Change the regex `countNonWhiteSpace` to look for multiple non-whitespace characters in a string.
修改正则表达式 `countNonWhiteSpace` 以查找字符串中的多个非空字符。
# --hints--
Your regex should use the global flag.
您的正则表达式应该使用全局标识。
```js
assert(countNonWhiteSpace.global);
```
Your regex should use the shorthand character `\S` to match all non-whitespace characters.
你的正则表达式应该使用简写字符 `\S` 来匹配所有非空白字符。
```js
assert(/\\S/.test(countNonWhiteSpace.source));
```
Your regex should find 35 non-spaces in `"Men are from Mars and women are from Venus."`
您的正则表达式应该在字符串 `Men are from Mars and women are from Venus.` 中找到 35 个非空格字符。
```js
assert(
@ -45,13 +47,13 @@ assert(
);
```
Your regex should find 23 non-spaces in `"Space: the final frontier."`
你的正则表达式应该在 `Space: the final frontier.` 中匹配到 23 个非空白字符。
```js
assert('Space: the final frontier.'.match(countNonWhiteSpace).length == 23);
```
Your regex should find 21 non-spaces in `"MindYourPersonalSpace"`
你的正则表达式应该在 `MindYourPersonalSpace` 中匹配到 21 个非空白字符。
```js
assert('MindYourPersonalSpace'.match(countNonWhiteSpace).length == 21);

View File

@ -1,6 +1,6 @@
---
id: 587d7db5367417b2b2512b97
title: Match Numbers and Letters of the Alphabet
title: 匹配字母表中的数字和字母
challengeType: 1
forumTopicId: 301356
dashedName: match-numbers-and-letters-of-the-alphabet
@ -8,38 +8,37 @@ dashedName: match-numbers-and-letters-of-the-alphabet
# --description--
Using the hyphen (`-`) to match a range of characters is not limited to letters. It also works to match a range of numbers.
使用连字符(`-`)匹配字符范围并不仅限于字母。 它还可以匹配一系列数字。
For example, `/[0-5]/` matches any number between `0` and `5`, including the `0` and `5`.
例如,`/[0-5]/` 匹配 `0` `5` 之间的任意数字,包含 `0` `5`
Also, it is possible to combine a range of letters and numbers in a single character set.
此外,还可以在单个字符集中组合一系列字母和数字。
```js
let jennyStr = "Jenny8675309";
let myRegex = /[a-z0-9]/ig;
// matches all letters and numbers in jennyStr
jennyStr.match(myRegex);
```
# --instructions--
Create a single regex that matches a range of letters between `h` and `s`, and a range of numbers between `2` and `6`. Remember to include the appropriate flags in the regex.
创建一个正则表达式,使其可以匹配 `h` `s` 之间的一系列字母,以及 `2` `6` 之间的一系列数字。 请记得在正则表达式中包含恰当的标志。
# --hints--
Your regex `myRegex` should match 17 items.
你的正则表达式 `myRegex` 应该匹配 17 项。
```js
assert(result.length == 17);
```
Your regex `myRegex` should use the global flag.
你的正则表达式 `myRegex` 应该使用全局标志。
```js
assert(myRegex.flags.match(/g/).length == 1);
```
Your regex `myRegex` should use the case insensitive flag.
你的正则表达式 `myRegex` 应该使用忽略大小写的标志。
```js
assert(myRegex.flags.match(/i/).length == 1);

View File

@ -1,6 +1,6 @@
---
id: 587d7db5367417b2b2512b95
title: Match Single Character with Multiple Possibilities
title: 将单个字符与多种可能性匹配
challengeType: 1
forumTopicId: 301357
dashedName: match-single-character-with-multiple-possibilities
@ -8,11 +8,11 @@ dashedName: match-single-character-with-multiple-possibilities
# --description--
You learned how to match literal patterns (`/literal/`) and wildcard character (`/./`). Those are the extremes of regular expressions, where one finds exact matches and the other matches everything. There are options that are a balance between the two extremes.
已经了解了文字匹配模式(`/literal/`)和通配符(`/./`)。 这是正则表达式的两种极端情况,一种是精确匹配,而另一种则是匹配所有。 在这两种极端情况之间有一个平衡选项。
You can search for a literal pattern with some flexibility with <dfn>character classes</dfn>. Character classes allow you to define a group of characters you wish to match by placing them inside square (`[` and `]`) brackets.
可以使用字符集 <dfn>character classes</dfn>)更灵活的匹配字符。 可以把字符集放在方括号(`[` `]`)之间来定义一组需要匹配的字符串。
For example, you want to match `"bag"`, `"big"`, and `"bug"` but not `"bog"`. You can create the regex `/b[aiu]g/` to do this. The `[aiu]` is the character class that will only match the characters `"a"`, `"i"`, or `"u"`.
例如,如果想要匹配 `bag``big` `bug`,但是不想匹配 `bog`。 可以创建正则表达式 `/b[aiu]g/` 来执行此操作。 `[aiu]` 是只匹配字符 `a``i` 或者 `u` 的字符集。
```js
let bigStr = "big";
@ -20,46 +20,47 @@ let bagStr = "bag";
let bugStr = "bug";
let bogStr = "bog";
let bgRegex = /b[aiu]g/;
bigStr.match(bgRegex); // Returns ["big"]
bagStr.match(bgRegex); // Returns ["bag"]
bugStr.match(bgRegex); // Returns ["bug"]
bogStr.match(bgRegex); // Returns null
bigStr.match(bgRegex);
bagStr.match(bgRegex);
bugStr.match(bgRegex);
bogStr.match(bgRegex);
```
按顺序排列,四次 `match` 调用将返回值 `["big"]``["bag"]``["bug"]``null`
# --instructions--
Use a character class with vowels (`a`, `e`, `i`, `o`, `u`) in your regex `vowelRegex` to find all the vowels in the string `quoteSample`.
使用元音字符集(`a``e``i``o``u`)在正则表达式 `vowelRegex` 中匹配到字符串 `quoteSample` 中的所有元音。
**Note**
Be sure to match both upper- and lowercase vowels.
**注意:**一定要同时匹配大小写元音。
# --hints--
You should find all 25 vowels.
你应该匹配到所有25个元音。
```js
assert(result.length == 25);
```
Your regex `vowelRegex` should use a character class.
你的正则表达式 `vowelRegex` 应该使用字符集。
```js
assert(/\[.*\]/.test(vowelRegex.source));
```
Your regex `vowelRegex` should use the global flag.
你的正则表达式 `vowelRegex` 应该使用全局标志。
```js
assert(vowelRegex.flags.match(/g/).length == 1);
```
Your regex `vowelRegex` should use the case insensitive flag.
你的正则表达式 `vowelRegex` 应该使用忽略大小写标志。
```js
assert(vowelRegex.flags.match(/i/).length == 1);
```
Your regex should not match any consonants.
你的正则表达式不应该匹配任何辅音。
```js
assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()));

View File

@ -1,6 +1,6 @@
---
id: 587d7db6367417b2b2512b98
title: Match Single Characters Not Specified
title: 匹配单个未指定的字符
challengeType: 1
forumTopicId: 301358
dashedName: match-single-characters-not-specified
@ -8,31 +8,31 @@ dashedName: match-single-characters-not-specified
# --description--
So far, you have created a set of characters that you want to match, but you could also create a set of characters that you do not want to match. These types of character sets are called <dfn>negated character sets</dfn>.
到目前为止,已经创建了一个想要匹配的字符集合,但也可以创建一个不想匹配的字符集合。 这些类型的字符集称为否定字符集( <dfn>negated character sets</dfn>)。
To create a negated character set, you place a caret character (`^`) after the opening bracket and before the characters you do not want to match.
要创建否定字符集,需要在开始括号后面和不想匹配的字符前面放置脱字符(即`^`)。
For example, `/[^aeiou]/gi` matches all characters that are not a vowel. Note that characters like `.`, `!`, `[`, `@`, `/` and white space are matched - the negated vowel character set only excludes the vowel characters.
例如,`/[^aeiou]/gi` 匹配所有非元音字符。 注意,字符 `.``!``[``@``/` 和空白字符等也会被匹配,该否定字符集仅排除元音字符。
# --instructions--
Create a single regex that matches all characters that are not a number or a vowel. Remember to include the appropriate flags in the regex.
创建一个匹配所有非数字或元音字符的正则表达式。 请记得在正则表达式中包含恰当的标志。
# --hints--
Your regex `myRegex` should match 9 items.
你的正则表达式 `myRegex` 应该匹配 9 项。
```js
assert(result.length == 9);
```
Your regex `myRegex` should use the global flag.
你的正则表达式 `myRegex` 应该使用全局标志。
```js
assert(myRegex.flags.match(/g/).length == 1);
```
Your regex `myRegex` should use the case insensitive flag.
你的正则表达式 `myRegex` 应该使用忽略大小写标志。
```js
assert(myRegex.flags.match(/i/).length == 1);

View File

@ -1,6 +1,6 @@
---
id: 587d7db8367417b2b2512ba3
title: Match Whitespace
title: 匹配空白字符
challengeType: 1
forumTopicId: 301359
dashedName: match-whitespace
@ -8,36 +8,36 @@ dashedName: match-whitespace
# --description--
The challenges so far have covered matching letters of the alphabet and numbers. You can also match the whitespace or spaces between letters.
迄今为止的挑战包括匹配字母和数字。 还可以匹配字符之间的空格。
You can search for whitespace using `\s`, which is a lowercase `s`. This pattern not only matches whitespace, but also carriage return, tab, form feed, and new line characters. You can think of it as similar to the character class `[ \r\t\f\n\v]`.
可以使用 `\s` 搜寻空格,其中 `s` 是小写。 此匹配模式将匹配空格、回车符、制表符、换页符和换行符。 可以认为这类似于元字符 `[ \r\t\f\n\v]`
```js
let whiteSpace = "Whitespace. Whitespace everywhere!"
let spaceRegex = /\s/g;
whiteSpace.match(spaceRegex);
// Returns [" ", " "]
```
这个 `match` 调用将返回 `[" ", " "]`
# --instructions--
Change the regex `countWhiteSpace` to look for multiple whitespace characters in a string.
修改正则表达式 `countWhiteSpace` 查找字符串中的多个空白字符。
# --hints--
Your regex should use the global flag.
你的正则表达式应该使用全局标识。
```js
assert(countWhiteSpace.global);
```
Your regex should use the shorthand character `\s` to match all whitespace characters.
正则表达式应该使用元字符 `\s` 匹配所有的空白。
```js
assert(/\\s/.test(countWhiteSpace.source));
```
Your regex should find eight spaces in `"Men are from Mars and women are from Venus."`
你的正则表达式应该在字符串 `Men are from Mars and women are from Venus.` 中找到 8 个非空格字符。
```js
assert(
@ -46,13 +46,13 @@ assert(
);
```
Your regex should find three spaces in `"Space: the final frontier."`
你的正则表达式应该在 `Space: the final frontier.` 中匹配到 3 个非空白字符。
```js
assert('Space: the final frontier.'.match(countWhiteSpace).length == 3);
```
Your regex should find no spaces in `"MindYourPersonalSpace"`
你的正则表达式在 `MindYourPersonalSpace` 中应该匹配不到空白字符。
```js
assert('MindYourPersonalSpace'.match(countWhiteSpace) == null);

View File

@ -1,6 +1,6 @@
---
id: 587d7dba367417b2b2512ba9
title: Positive and Negative Lookahead
title: 正向先行断言和负向先行断言
challengeType: 1
forumTopicId: 301360
dashedName: positive-and-negative-lookahead
@ -8,88 +8,90 @@ dashedName: positive-and-negative-lookahead
# --description--
<dfn>Lookaheads</dfn> are patterns that tell JavaScript to look-ahead in your string to check for patterns further along. This can be useful when you want to search for multiple patterns over the same string.
先行断言 <dfn>Lookaheads</dfn>)是告诉 JavaScript 在字符串中向前查找的匹配模式。 当想要在同一个字符串上搜寻多个匹配模式时,这可能非常有用。
There are two kinds of lookaheads: <dfn>positive lookahead</dfn> and <dfn>negative lookahead</dfn>.
有两种先行断言:正向先行断言(<dfn>positive lookahead</dfn>)和负向先行断言(<dfn>negative lookahead</dfn>)。
A positive lookahead will look to make sure the element in the search pattern is there, but won't actually match it. A positive lookahead is used as `(?=...)` where the `...` is the required part that is not matched.
正向先行断言会查看并确保搜索匹配模式中的元素存在,但实际上并不匹配。 正向先行断言的用法是 `(?=...)`,其中 `...` 就是需要存在但不会被匹配的部分。
On the other hand, a negative lookahead will look to make sure the element in the search pattern is not there. A negative lookahead is used as `(?!...)` where the `...` is the pattern that you do not want to be there. The rest of the pattern is returned if the negative lookahead part is not present.
另一方面,负向先行断言会查看并确保搜索匹配模式中的元素不存在。 负向先行断言的用法是 `(?!...)`,其中 `...` 是希望不存在的匹配模式。 如果负向先行断言部分不存在,将返回匹配模式的其余部分。
Lookaheads are a bit confusing but some examples will help.
尽管先行断言有点儿令人困惑,但是这些示例会有所帮助。
```js
let quit = "qu";
let noquit = "qt";
let quRegex= /q(?=u)/;
let qRegex = /q(?!u)/;
quit.match(quRegex); // Returns ["q"]
noquit.match(qRegex); // Returns ["q"]
quit.match(quRegex);
noquit.match(qRegex);
```
A more practical use of lookaheads is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:
这两次 `match` 调用都将返回 `["q"]`
先行断言的更实际用途是检查一个字符串中的两个或更多匹配模式。 这里有一个简单的密码检查器,密码规则是 3 到 6 个字符且至少包含一个数字:
```js
let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true
checkPass.test(password);
```
# --instructions--
Use lookaheads in the `pwRegex` to match passwords that are greater than 5 characters long, and have two consecutive digits.
在正则表达式 `pwRegex` 中使用先行断言以匹配大于 5 个字符且有两个连续数字的密码。
# --hints--
Your regex should use two positive `lookaheads`.
你的正则表达式应该使用两个正向先行断言( `lookaheads`)。
```js
assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
```
Your regex should not match `"astronaut"`
您的正则表达式不应匹配字符串 `astronaut`
```js
assert(!pwRegex.test('astronaut'));
```
Your regex should not match `"banan1"`
你的正则表达式不应匹配字符串 `banan1`
```js
assert(!pwRegex.test('banan1'));
```
Your regex should match `"bana12"`
你的正则表达式应该匹配字符串 `bana12`
```js
assert(pwRegex.test('bana12'));
```
Your regex should match `"abc123"`
你的正则表达式应该匹配字符串 `abc123`
```js
assert(pwRegex.test('abc123'));
```
Your regex should not match `"12345"`
你的正则表达式不应匹配字符串 `12345`
```js
assert(!pwRegex.test('12345'));
```
Your regex should match `"8pass99"`
你的正则表达式应该匹配字符串 `8pass99`
```js
assert(pwRegex.test('8pass99'));
```
Your regex should not match `"1a2bcde"`
你的正表达式不应匹配字符串 `1a2bcde`
```js
assert(!pwRegex.test('1a2bcde'));
```
Your regex should match `"astr1on11aut"`
你的正则表达式应该匹配字符串 `astr1on11aut`
```js
assert(pwRegex.test('astr1on11aut'));

View File

@ -1,6 +1,6 @@
---
id: 587d7dbb367417b2b2512bac
title: Remove Whitespace from Start and End
title: 删除开头和结尾的空白
challengeType: 1
forumTopicId: 301362
dashedName: remove-whitespace-from-start-and-end
@ -8,29 +8,29 @@ dashedName: remove-whitespace-from-start-and-end
# --description--
Sometimes whitespace characters around strings are not wanted but are there. Typical processing of strings is to remove the whitespace at the start and end of it.
有时字符串周围存在的空白字符并不是必需的。 字符串的典型处理是删除字符串开头和结尾处的空格。
# --instructions--
Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
编写一个正则表达式并使用适当的字符串方法删除字符串开头和结尾的空格。
**Note:** The `String.prototype.trim()` method would work here, but you'll need to complete this challenge using regular expressions.
**注意:** `String.prototype.trim()` 方法在这里也可以实现同样的效果,但是你需要使用正则表达式来完成此项挑战。
# --hints--
`result` should equal to `"Hello, World!"`
`result` 应该等于 `Hello, World!`
```js
assert(result == 'Hello, World!');
```
Your solution should not use the `String.prototype.trim()` method.
你不应该使用 `String.prototype.trim()` 方法。
```js
assert(!code.match(/\.?[\s\S]*?trim/));
```
The `result` variable should not be set equal to a string.
`result` 变量不应该设置为等于字符串。
```js
assert(!code.match(/result\s*=\s*".*?"/));

View File

@ -1,6 +1,6 @@
---
id: 587d7db8367417b2b2512ba2
title: Restrict Possible Usernames
title: 限制可能的用户名
challengeType: 1
forumTopicId: 301363
dashedName: restrict-possible-usernames
@ -8,96 +8,102 @@ dashedName: restrict-possible-usernames
# --description--
Usernames are used everywhere on the internet. They are what give users a unique identity on their favorite sites.
用户名在互联网上随处可见。 它们是用户在自己喜欢的网站上的唯一身份。
You need to check all the usernames in a database. Here are some simple rules that users have to follow when creating their username.
需要检索数据库中的所有用户名。 以下是用户在创建用户名时必须遵守的一些简单规则。
1) Usernames can only use alpha-numeric characters.
1) 用户名只能是数字字母字符。
2) The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
2) 用户名中的数字必须在最后。 数字可以有零个或多个。 用户名不能以数字开头。
3) Username letters can be lowercase and uppercase.
3) 用户名字母可以是小写字母和大写字母。
4) Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
4) 用户名长度必须至少为两个字符。 两位用户名只能使用字母。
# --instructions--
Change the regex `userCheck` to fit the constraints listed above.
修改正则表达式 `userCheck` 以满足上面列出的约束。
# --hints--
Your regex should match `JACK`
你的正则表达式应该匹配字符串 `JACK`
```js
assert(userCheck.test('JACK'));
```
Your regex should not match `J`
你的正则表达式不应匹配字符串 `J`
```js
assert(!userCheck.test('J'));
```
Your regex should match `Jo`
你的正则表达式应该匹配字符串 `Jo`
```js
assert(userCheck.test('Jo'));
```
Your regex should match `Oceans11`
你的正则表达式应该匹配字符串 `Oceans11`
```js
assert(userCheck.test('Oceans11'));
```
Your regex should match `RegexGuru`
你的正则表达式应该匹配字符串 `RegexGuru`
```js
assert(userCheck.test('RegexGuru'));
```
Your regex should not match `007`
你的正则表达式不应匹配字符串 `007`
```js
assert(!userCheck.test('007'));
```
Your regex should not match `9`
你的正则表达式不应匹配字符串 `9`
```js
assert(!userCheck.test('9'));
```
Your regex should not match `A1`
你的正则表达式不应匹配字符串 `A1`
```js
assert(!userCheck.test('A1'));
```
Your regex should not match `BadUs3rnam3`
你的正则表达式不应匹配字符串 `BadUs3rnam3`
```js
assert(!userCheck.test('BadUs3rnam3'));
```
Your regex should match `Z97`
你的正则表达式应该匹配字符串 `Z97`
```js
assert(userCheck.test('Z97'));
```
Your regex should not match `c57bT3`
你的正则表达式不应匹配字符串 `c57bT3`
```js
assert(!userCheck.test('c57bT3'));
```
Your regex should match `AB1`
你的正则表达式应该匹配字符串 `AB1`
```js
assert(userCheck.test('AB1'));
```
你的正则表达式不应匹配字符串 `J%4`
```js
assert(!userCheck.test('J%4'))
```
# --seed--
## --seed-contents--

View File

@ -1,6 +1,6 @@
---
id: 587d7db9367417b2b2512ba7
title: Specify Exact Number of Matches
title: 指定匹配的确切数量
challengeType: 1
forumTopicId: 301365
dashedName: specify-exact-number-of-matches
@ -8,63 +8,65 @@ dashedName: specify-exact-number-of-matches
# --description--
You can specify the lower and upper number of patterns with quantity specifiers using curly brackets. Sometimes you only want a specific number of matches.
可以使用带有花括号的数量说明符来指定匹配模式的上下限。 但有时只需要特定数量的匹配。
To specify a certain number of patterns, just have that one number between the curly brackets.
要指定一定数量的匹配模式,只需在大括号之间放置一个数字。
For example, to match only the word `"hah"` with the letter `a` `3` times, your regex would be `/ha{3}h/`.
例如,要只匹配字母 `a` 出现 `3` 次的单词`hah`,正则表达式应为`/ha{3}h/`
```js
let A4 = "haaaah";
let A3 = "haaah";
let A100 = "h" + "a".repeat(100) + "h";
let multipleHA = /ha{3}h/;
multipleHA.test(A4); // Returns false
multipleHA.test(A3); // Returns true
multipleHA.test(A100); // Returns false
multipleHA.test(A4);
multipleHA.test(A3);
multipleHA.test(A100);
```
按顺序排列,三次 `test` 调用将返回值 `false``true``false`
# --instructions--
Change the regex `timRegex` to match the word `"Timber"` only when it has four letter `m`'s.
修改正则表达式`timRegex`,以匹配仅有四个字母 `m` 的单词 `Timber`
# --hints--
Your regex should use curly brackets.
你的正则表达式应该使用花括号。
```js
assert(timRegex.source.match(/{.*?}/).length > 0);
```
Your regex should not match `"Timber"`
你的正则表达式不应匹配字符串 `Timber`
```js
timRegex.lastIndex = 0;
assert(!timRegex.test('Timber'));
```
Your regex should not match `"Timmber"`
你的正则表达式不应匹配字符串 `Timmber`
```js
timRegex.lastIndex = 0;
assert(!timRegex.test('Timmber'));
```
Your regex should not match `"Timmmber"`
你的正则表达式不应匹配字符串 `Timmmber`
```js
timRegex.lastIndex = 0;
assert(!timRegex.test('Timmmber'));
```
Your regex should match `"Timmmmber"`
你的正则表达式应该匹配字符串 `Timmmmber`
```js
timRegex.lastIndex = 0;
assert(timRegex.test('Timmmmber'));
```
Your regex should not match `"Timber"` with 30 `m`'s in it.
你的正则表达式不应该匹配包含 30 个字母 `m``Timber`
```js
timRegex.lastIndex = 0;

View File

@ -1,6 +1,6 @@
---
id: 587d7db9367417b2b2512ba6
title: Specify Only the Lower Number of Matches
title: 只指定匹配的下限
challengeType: 1
forumTopicId: 301366
dashedName: specify-only-the-lower-number-of-matches
@ -8,65 +8,67 @@ dashedName: specify-only-the-lower-number-of-matches
# --description--
You can specify the lower and upper number of patterns with quantity specifiers using curly brackets. Sometimes you only want to specify the lower number of patterns with no upper limit.
可以使用带有花括号的数量说明符来指定匹配模式的上下限。 但有时候只想指定匹配模式的下限而不需要指定上限。
To only specify the lower number of patterns, keep the first number followed by a comma.
为此,在第一个数字后面跟一个逗号即可。
For example, to match only the string `"hah"` with the letter `a` appearing at least `3` times, your regex would be `/ha{3,}h/`.
例如,要匹配至少出现 `3` 次字母 `a` 的字符串 `hah`,正则表达式应该是 `/ha{3,}h/`
```js
let A4 = "haaaah";
let A2 = "haah";
let A100 = "h" + "a".repeat(100) + "h";
let multipleA = /ha{3,}h/;
multipleA.test(A4); // Returns true
multipleA.test(A2); // Returns false
multipleA.test(A100); // Returns true
multipleA.test(A4);
multipleA.test(A2);
multipleA.test(A100);
```
按顺序排列,三次 `test` 调用将返回值 `true``false``true`
# --instructions--
Change the regex `haRegex` to match the word `"Hazzah"` only when it has four or more letter `z`'s.
修改正则表达式 `haRegex`,匹配包含四个或更多字母 `z` 的单词 `Hazzah`
# --hints--
Your regex should use curly brackets.
你的正则表达式应该使用花括号。
```js
assert(haRegex.source.match(/{.*?}/).length > 0);
```
Your regex should not match `"Hazzah"`
你的正则表达式不应匹配字符串 `Hazzah`
```js
assert(!haRegex.test('Hazzah'));
```
Your regex should not match `"Hazzzah"`
你的正则表达式不应匹配字符串 `Hazzzah`
```js
assert(!haRegex.test('Hazzzah'));
```
Your regex should match `"Hazzzzah"`
你的正则表达式应该匹配字符串 `Hazzzzah`
```js
assert('Hazzzzah'.match(haRegex)[0].length === 8);
```
Your regex should match `"Hazzzzzah"`
你的正则表达式应该匹配字符串 `Hazzzzzah`
```js
assert('Hazzzzzah'.match(haRegex)[0].length === 9);
```
Your regex should match `"Hazzzzzzah"`
你的正则表达式应该匹配字符串 `Hazzzzzzah`
```js
assert('Hazzzzzzah'.match(haRegex)[0].length === 10);
```
Your regex should match `"Hazzah"` with 30 `z`'s in it.
你的正则表达式应该匹配包含 30 个字母 `z``Hazzah`
```js
assert('Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah'.match(haRegex)[0].length === 34);

View File

@ -1,6 +1,6 @@
---
id: 587d7db9367417b2b2512ba5
title: Specify Upper and Lower Number of Matches
title: 指定匹配的上限和下限
challengeType: 1
forumTopicId: 301367
dashedName: specify-upper-and-lower-number-of-matches
@ -8,63 +8,65 @@ dashedName: specify-upper-and-lower-number-of-matches
# --description--
Recall that you use the plus sign `+` to look for one or more characters and the asterisk `*` to look for zero or more characters. These are convenient but sometimes you want to match a certain range of patterns.
回想一下,使用加号 `+` 查找一个或多个字符,使用星号 `*` 查找零个或多个字符。 这些都很方便,但有时需要匹配一定范围的匹配模式。
You can specify the lower and upper number of patterns with <dfn>quantity specifiers</dfn>. Quantity specifiers are used with curly brackets (`{` and `}`). You put two numbers between the curly brackets - for the lower and upper number of patterns.
可以使用数量说明符(<dfn>quantity specifiers</dfn>)指定匹配模式的上下限。 数量说明符与花括号(`{` `}`)一起使用。 可以在花括号之间放两个数字,这两个数字代表匹配模式的上限和下限。
For example, to match only the letter `a` appearing between `3` and `5` times in the string `"ah"`, your regex would be `/a{3,5}h/`.
例如,要匹配出现 `3` `5` 次字母 `a` 的在字符串 `ah`,正则表达式应为`/a{3,5}h/`
```js
let A4 = "aaaah";
let A2 = "aah";
let multipleA = /a{3,5}h/;
multipleA.test(A4); // Returns true
multipleA.test(A2); // Returns false
multipleA.test(A4);
multipleA.test(A2);
```
第一次 `test` 调用将返回 `true`,而第二次调用将返回 `false`
# --instructions--
Change the regex `ohRegex` to match the entire phrase `"Oh no"` only when it has `3` to `6` letter `h`'s.
修改正则表达式 `ohRegex` 以匹配出现 `3` `6` 次字母 `h` 的字符串 `Oh no`
# --hints--
Your regex should use curly brackets.
你的正则表达式应该使用花括号。
```js
assert(ohRegex.source.match(/{.*?}/).length > 0);
```
Your regex should not match `"Ohh no"`
你的正则表达式不应匹配字符串 `Ohh no`
```js
assert(!ohRegex.test('Ohh no'));
```
Your regex should match `"Ohhh no"`
你的正则表达式应该匹配字符串 `Ohhh no`
```js
assert('Ohhh no'.match(ohRegex)[0].length === 7);
```
Your regex should match `"Ohhhh no"`
你的正则表达式应该匹配字符串 `Ohhhh no`
```js
assert('Ohhhh no'.match(ohRegex)[0].length === 8);
```
Your regex should match `"Ohhhhh no"`
你的正则表达式应该匹配字符串 `Ohhhhh no`
```js
assert('Ohhhhh no'.match(ohRegex)[0].length === 9);
```
Your regex should match `"Ohhhhhh no"`
你的正则表达式应该匹配字符串 `Ohhhhhh no`
```js
assert('Ohhhhhh no'.match(ohRegex)[0].length === 10);
```
Your regex should not match `"Ohhhhhhh no"`
你的正则表达式应该匹配字符串 `Ohhhhhhh no`
```js
assert(!ohRegex.test('Ohhhhhhh no'));

View File

@ -1,6 +1,6 @@
---
id: 587d7dbb367417b2b2512bab
title: Use Capture Groups to Search and Replace
title: 使用捕获组搜索和替换
challengeType: 1
forumTopicId: 301368
dashedName: use-capture-groups-to-search-and-replace
@ -8,55 +8,57 @@ dashedName: use-capture-groups-to-search-and-replace
# --description--
Searching is useful. However, you can make searching even more powerful when it also changes (or replaces) the text you match.
搜索功能是很有用的。 但是,当搜索同时也执行更改(或替换)匹配文本的操作时,搜索功能就会显得更加强大。
You can search and replace text in a string using `.replace()` on a string. The inputs for `.replace()` is first the regex pattern you want to search for. The second parameter is the string to replace the match or a function to do something.
可以在字符串上使用 `.replace()` 方法来搜索并替换字符串中的文本。 `.replace()` 的输入首先是想要搜索的正则表达式匹配模式。 第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。
```js
let wrongText = "The sky is silver.";
let silverRegex = /silver/;
wrongText.replace(silverRegex, "blue");
// Returns "The sky is blue."
```
You can also access capture groups in the replacement string with dollar signs (`$`).
`replace` 调用将返回字符串 `The sky is blue.`
你还可以使用美元符号(`$`)访问替换字符串中的捕获组。
```js
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"
```
调用 `replace` 将返回字符串 `Camp Code`
# --instructions--
Write a regex `fixRegex` using three capture groups that will search for each word in the string "one two three". Then update the `replaceText` variable to replace "one two three" with the string "three two one" and assign the result to the `result` variable. Make sure you are utilizing capture groups in the replacement string using the dollar sign (`$`) syntax.
使用三个捕获组编写一个正则表达式 `fixRegex`,这三个捕获组将搜索字符串 `one two three` 中的每个单词。 然后更新 `replaceText` 变量,以字符串 `three two one` 替换 `one two three`,并将结果分配给 `result` 变量。 确保使用美元符号(`$`)语法在替换字符串中使用捕获组。
# --hints--
You should use `.replace()` to search and replace.
你应该使用 `.replace()` 搜索并替换。
```js
assert(code.match(/\.replace\(.*\)/));
```
Your regex should change `"one two three"` to `"three two one"`
你的正则表达式应该将字符串 `one two three` 更改为字符串 `three two one`
```js
assert(result === 'three two one');
```
You should not change the last line.
你不应该改变最后一行。
```js
assert(code.match(/result\s*=\s*str\.replace\(.*?\)/));
```
`fixRegex` should use at least three capture groups.
`fixRegex` 应该至少使用三个抓取组。
```js
assert(new RegExp(fixRegex.source + '|').exec('').length - 1 >= 3);
```
`replaceText` should use parenthesized submatch string(s) (i.e. the nth parenthesized submatch string, $n, corresponds to the nth capture group).
`replaceText` 应该使用括号化的子匹配字符串例如nth 括号化的子匹配字符串, $n, 对应于第 n 个捕获组)。
```js
{

View File

@ -1,6 +1,6 @@
---
id: 587d7db3367417b2b2512b8e
title: Using the Test Method
title: 使用测试方法
challengeType: 1
forumTopicId: 301369
dashedName: using-the-test-method
@ -8,32 +8,33 @@ dashedName: using-the-test-method
# --description--
Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching.
在编程语言中,正则表达式用于匹配指定的字符串。 通过正则表达式创建匹配模式(规则)可以帮你完成指定匹配。
If you want to find the word `"the"` in the string `"The dog chased the cat"`, you could use the following regular expression: `/the/`. Notice that quote marks are not required within the regular expression.
如果想要在字符串 `The dog chased the cat` 中匹配到 `the` 这个单词,可以使用如下正则表达式:`/the/`。 注意,正则表达式中不需要引号。
JavaScript has multiple ways to use regexes. One way to test a regex is using the `.test()` method. The `.test()` method takes the regex, applies it to a string (which is placed inside the parentheses), and returns `true` or `false` if your pattern finds something or not.
JavaScript 中有多种使用正则表达式的方法。 测试正则表达式的一种方法是使用 `.test()` 方法。 `.test()` 方法会把编写的正则表达式和字符串(即括号内的内容)匹配,如果成功匹配到字符,则返回 `true`,反之,返回 `false`
```js
let testStr = "freeCodeCamp";
let testRegex = /Code/;
testRegex.test(testStr);
// Returns true
```
`test` 方法会返回 `true`
# --instructions--
Apply the regex `myRegex` on the string `myString` using the `.test()` method.
使用 `.test()` 方法,检测字符串 `myString` 是否符合正则表达式 `myRegex` 定义的规则。
# --hints--
You should use `.test()` to test the regex.
你应该使用 `.test()` 方法来检测正则表达式。
```js
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
```
Your result should return `true`.
你的返回结果应该为 `true`
```js
assert(result === true);