chore(i8n,learn): processed translations
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
15047f2d90
commit
e5c44a3ae5
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7dba367417b2b2512ba8
|
||||
title: 检查全部或无
|
||||
title: Check for All or None
|
||||
challengeType: 1
|
||||
forumTopicId: 301338
|
||||
dashedName: check-for-all-or-none
|
||||
@ -8,11 +8,11 @@ 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";
|
||||
@ -24,31 +24,35 @@ rainbowRegex.test(british); // Returns true
|
||||
|
||||
# --instructions--
|
||||
|
||||
修改正则表达式`favRegex`以匹配美式英语(favorite)和英式英语(favourite)的单词版本。
|
||||
Change the regex `favRegex` to match both the American English (favorite) and the British English (favourite) version of the word.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用可选符号`?`。
|
||||
Your regex should use the optional symbol, `?`.
|
||||
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
assert(favRegex.source.match(/\?/).length > 0);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'favorite'`。
|
||||
Your regex should match `"favorite"`
|
||||
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
assert(favRegex.test('favorite'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'favourite'`。
|
||||
Your regex should match `"favourite"`
|
||||
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
assert(favRegex.test('favourite'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'fav'`。
|
||||
Your regex should not match `"fav"`
|
||||
|
||||
```js
|
||||
favRegex.lastIndex = 0;
|
||||
assert(!favRegex.test('fav'));
|
||||
```
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 5c3dda8b4d8df89bea71600f
|
||||
title: 检查混合字符组
|
||||
title: Check For Mixed Grouping of Characters
|
||||
challengeType: 1
|
||||
forumTopicId: 301339
|
||||
dashedName: check-for-mixed-grouping-of-characters
|
||||
@ -8,55 +8,62 @@ 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 `()`.
|
||||
|
||||
如果想在字符串找到 `Penguin` 或 `Pumpkin`,可以这个正则表达式:`/P(engu|umpk)in/g`。
|
||||
If you want to find either `Penguin` or `Pumpkin` in a string, you can use the following Regular Expression: `/P(engu|umpk)in/g`
|
||||
|
||||
然后使用 `test()` 方法检查 test 字符串里面是否包含字符组。
|
||||
Then check whether the desired string groups are in the test string by using the `test()` method.
|
||||
|
||||
```js
|
||||
let testStr = "Pumpkin";
|
||||
let testRegex = /P(engu|umpk)in/g;
|
||||
let testRegex = /P(engu|umpk)in/;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
完善正则表达式,使其以区分大小写的方式检查 `Franklin Roosevelt` 或 `Eleanor Roosevelt` 的名字,并且应该忽略 middle names。
|
||||
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.
|
||||
|
||||
然后完善代码,使创建的正则检查 `myString`,根据正则是否匹配返回 `true` 或 `false`。
|
||||
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.
|
||||
|
||||
# --hints--
|
||||
|
||||
正则 `myRegex` 测试 `Franklin D. Roosevelt` 应该返回 `true`。
|
||||
Your regex `myRegex` should return `true` for the string `Franklin D. Roosevelt`
|
||||
|
||||
```js
|
||||
myRegex.lastIndex = 0;
|
||||
assert(myRegex.test('Franklin D. Roosevelt'));
|
||||
```
|
||||
|
||||
正则 `myRegex` 测试 `Eleanor Roosevelt` 应该返回 `true`。
|
||||
Your regex `myRegex` should return `true` for the string `Eleanor Roosevelt`
|
||||
|
||||
```js
|
||||
myRegex.lastIndex = 0;
|
||||
assert(myRegex.test('Eleanor Roosevelt'));
|
||||
```
|
||||
|
||||
正则 `myRegex` 测试 `Franklin Rosevelt` 应该返回 `false`。
|
||||
Your regex `myRegex` should return `false` for the string `Franklin Rosevelt`
|
||||
|
||||
```js
|
||||
myRegex.lastIndex = 0;
|
||||
assert(!myRegex.test('Franklin Rosevelt'));
|
||||
```
|
||||
|
||||
应该使用 `.test()` 来测试正则。
|
||||
Your regex `myRegex` should return `false` for the string `Frank Roosevelt`
|
||||
|
||||
```js
|
||||
myRegex.lastIndex = 0;
|
||||
assert(!myRegex.test('Frank Roosevelt'));
|
||||
```
|
||||
|
||||
You should use `.test()` to test the regex.
|
||||
|
||||
```js
|
||||
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
```
|
||||
|
||||
result 应该返回 `true`。
|
||||
Your result should return `true`.
|
||||
|
||||
```js
|
||||
assert(result === true);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db4367417b2b2512b92
|
||||
title: 提取匹配项
|
||||
title: Extract Matches
|
||||
challengeType: 1
|
||||
forumTopicId: 301340
|
||||
dashedName: extract-matches
|
||||
@ -8,9 +8,11 @@ dashedName: extract-matches
|
||||
|
||||
# --description--
|
||||
|
||||
到目前为止,只是检查了一个匹配模式是否存在于字符串中。还可以使用`.match()`方法来提取找到的实际匹配项。
|
||||
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.
|
||||
|
||||
Here's an example:
|
||||
|
||||
```js
|
||||
"Hello, World!".match(/Hello/);
|
||||
@ -21,25 +23,32 @@ ourStr.match(ourRegex);
|
||||
// Returns ["expressions"]
|
||||
```
|
||||
|
||||
Note that the `.match` syntax is the "opposite" of the `.test` method you have been using thus far:
|
||||
|
||||
```js
|
||||
'string'.match(/regex/);
|
||||
/regex/.test('string');
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
利用`.match()`方法提取单词`coding`。
|
||||
Apply the `.match()` method to extract the word `coding`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`结果`应该包含单词`coding`。
|
||||
The `result` should have the word `coding`
|
||||
|
||||
```js
|
||||
assert(result.join() === 'coding');
|
||||
```
|
||||
|
||||
你的正则表达式`codingRegex`应该搜寻`coding`。
|
||||
Your regex `codingRegex` should search for `coding`
|
||||
|
||||
```js
|
||||
assert(codingRegex.source === 'coding');
|
||||
```
|
||||
|
||||
你应该使用`.match()`方法。
|
||||
You should use the `.match()` method.
|
||||
|
||||
```js
|
||||
assert(code.match(/\.match\(.*\)/));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db6367417b2b2512b9b
|
||||
title: 用惰性匹配来查找字符
|
||||
title: Find Characters with Lazy Matching
|
||||
challengeType: 1
|
||||
forumTopicId: 301341
|
||||
dashedName: find-characters-with-lazy-matching
|
||||
@ -8,29 +8,41 @@ 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.
|
||||
|
||||
可以将正则表达式`/t[a-z]*i/`应用于字符串`"titanic"`。这个正则表达式是一个以`t`开始,以`i`结束,并且中间有一些字母的匹配模式。
|
||||
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.
|
||||
|
||||
正则表达式默认是`贪婪`匹配,因此匹配返回为`["titani"]`。它会匹配到适合该匹配模式的最大子字符串。
|
||||
Regular expressions are by default greedy, so the match would return `["titani"]`. It finds the largest sub-string possible to fit the pattern.
|
||||
|
||||
但是,你可以使用`?`字符来将其变成`懒惰`匹配。调整后的正则表达式`/t[a-z]*?i/`匹配字符串`"titanic"`返回`["ti"]`。
|
||||
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"]`.
|
||||
|
||||
**注意**
|
||||
应该避免使用正则表达式解析 HTML,但是可以用正则表达式匹配 HTML 字符串。
|
||||
**Note**
|
||||
Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.
|
||||
|
||||
# --instructions--
|
||||
|
||||
修复正则表达式`/<.*>/`,让它返回 HTML 标签`<h1>`,而不是文本`"<h1>Winter is coming</h1>"`。请记得在正则表达式中使用通配符`.`来匹配任意字符。
|
||||
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.
|
||||
|
||||
# --hints--
|
||||
|
||||
`结果`变量应该是一个包含`<h1>`的数组。
|
||||
The `result` variable should be an array with `<h1>` in it
|
||||
|
||||
```js
|
||||
assert(result[0] == '<h1>');
|
||||
```
|
||||
|
||||
`myRegex` should use lazy matching
|
||||
|
||||
```js
|
||||
assert(/\?/g.test(myRegex));
|
||||
```
|
||||
|
||||
`myRegex` should not include the string 'h1'
|
||||
|
||||
```js
|
||||
assert(!myRegex.source.match('h1'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db4367417b2b2512b93
|
||||
title: 全局匹配
|
||||
title: Find More Than the First Match
|
||||
challengeType: 1
|
||||
forumTopicId: 301342
|
||||
dashedName: find-more-than-the-first-match
|
||||
@ -8,7 +8,7 @@ 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";
|
||||
@ -17,7 +17,7 @@ testStr.match(ourRegex);
|
||||
// Returns ["Repeat"]
|
||||
```
|
||||
|
||||
若要多次搜寻或提取模式匹配,可以使用`g`标志。
|
||||
To search or extract a pattern more than once, you can use the `g` flag.
|
||||
|
||||
```js
|
||||
let repeatRegex = /Repeat/g;
|
||||
@ -27,26 +27,26 @@ testStr.match(repeatRegex);
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用正则表达式`starRegex`,从字符串`twinkleStar`中匹配到所有的`"Twinkle"`单词并提取出来。
|
||||
Using the regex `starRegex`, find and extract both `"Twinkle"` words from the string `twinkleStar`.
|
||||
|
||||
**注意:**
|
||||
在正则表达式上可以有多个标志,比如`/search/gi`。
|
||||
**Note**
|
||||
You can have multiple flags on your regex like `/search/gi`
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式`starRegex`应该使用全局标志`g`。
|
||||
Your regex `starRegex` should use the global flag `g`
|
||||
|
||||
```js
|
||||
assert(starRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式`starRegex`应该使用忽略大小写标志`i`。
|
||||
Your regex `starRegex` should use the case insensitive flag `i`
|
||||
|
||||
```js
|
||||
assert(starRegex.flags.match(/i/).length == 1);
|
||||
```
|
||||
|
||||
你的匹配应该匹配单词`'Twinkle'`的两个匹配项。
|
||||
Your match should match both occurrences of the word `"Twinkle"`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -58,7 +58,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的匹配`结果`应该包含两个元素。
|
||||
Your match `result` should have two elements in it.
|
||||
|
||||
```js
|
||||
assert(result.length == 2);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db7367417b2b2512b9c
|
||||
title: 在狩猎中找到一个或多个罪犯
|
||||
title: Find One or More Criminals in a Hunt
|
||||
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:
|
||||
|
||||
当字母`z`在一行中出现一次或连续多次时,正则表达式`/z+/`会匹配到它。它会在以下所有字符串中找到匹配项:
|
||||
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:
|
||||
|
||||
```js
|
||||
"z"
|
||||
@ -22,7 +22,7 @@ dashedName: find-one-or-more-criminals-in-a-hunt
|
||||
"abczzzzzzzzzzzzzzzzzzzzzabc"
|
||||
```
|
||||
|
||||
但是它不会在以下字符串中找到匹配项,因为它们中没有字母`z`:
|
||||
But it does not find matches in the following strings since there are no letter `z` characters:
|
||||
|
||||
```js
|
||||
""
|
||||
@ -32,23 +32,23 @@ dashedName: find-one-or-more-criminals-in-a-hunt
|
||||
|
||||
# --instructions--
|
||||
|
||||
编写一个`贪婪`正则表达式,在一组其他人中匹配到一个或多个罪犯。罪犯由大写字母`C`表示。
|
||||
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`.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该匹配`'C'`中的 <em>一个</em> 罪犯('`C`')。
|
||||
Your regex should match one criminal (`C`) in `"C"`
|
||||
|
||||
```js
|
||||
assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'CC'`中的 <em>两个</em> 罪犯('`CC`')。
|
||||
Your regex should match two criminals (`CC`) in `"CC"`
|
||||
|
||||
```js
|
||||
assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'P1P5P4CCCP2P6P3'`中的 <em>三个</em> 罪犯('`CCC`')。
|
||||
Your regex should match three criminals (`CCC`) in `"P1P5P4CCCP2P6P3"`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -57,7 +57,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'P6P2P7P4P5CCCCCP3P1'`中的 <em>五个</em> 罪犯('`CCCCC`')。
|
||||
Your regex should match five criminals (`CCCCC`) in `"P6P2P7P4P5CCCCCP3P1"`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -66,19 +66,19 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式在`''`中不应该匹配到任何罪犯。
|
||||
Your regex should not match any criminals in `""`
|
||||
|
||||
```js
|
||||
assert(!reCriminals.test(''));
|
||||
```
|
||||
|
||||
你的正则表达式在`'P1P2P3'`中不应该匹配到任何罪犯。
|
||||
Your regex should not match any criminals in `"P1P2P3"`
|
||||
|
||||
```js
|
||||
assert(!reCriminals.test('P1P2P3'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'`中的 <em>五十个</em> 罪犯('`CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC`')。
|
||||
Your regex should match fifty criminals (`CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC`) in `"P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db4367417b2b2512b91
|
||||
title: 匹配时忽略大小写
|
||||
title: Ignore Case While Matching
|
||||
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.
|
||||
|
||||
大小写即大写字母和小写字母。大写字母如`"A"`、`"B"`和`"C"`。小写字母如`"a"`、`"b"`和`"c"`。
|
||||
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"`.
|
||||
|
||||
可以使用标志(flag)来匹配这两种情况。标志有很多,不过这里我们只关注忽略大小写的标志——`i`。可以通过将它附加到正则表达式之后来使用它。这里给出使用该标志的一个实例`/ignorecase/i`。这个字符串可以匹配字符串`"ignorecase"`、`"igNoreCase"`和`"IgnoreCase"`。
|
||||
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"`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
编写正则表达式`fccRegex`以匹配`"freeCodeCamp"`,忽略大小写。正则表达式不应与任何缩写或带有空格的变体匹配。
|
||||
Write a regex `fccRegex` to match `"freeCodeCamp"`, no matter its case. Your regex should not match any abbreviations or variations with spaces.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该匹配`freeCodeCamp`。
|
||||
Your regex should match `freeCodeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('freeCodeCamp'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FreeCodeCamp`。
|
||||
Your regex should match `FreeCodeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreeCodeCamp'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FreecodeCamp`。
|
||||
Your regex should match `FreecodeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreecodeCamp'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FreeCodecamp`。
|
||||
Your regex should match `FreeCodecamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreeCodecamp'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`Free Code Camp`。
|
||||
Your regex should not match `Free Code Camp`
|
||||
|
||||
```js
|
||||
assert(!fccRegex.test('Free Code Camp'));
|
||||
```
|
||||
|
||||
Your regex should match`FreeCOdeCamp`。
|
||||
Your regex should match `FreeCOdeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreeCOdeCamp'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`FCC`。
|
||||
Your regex should not match `FCC`
|
||||
|
||||
```js
|
||||
assert(!fccRegex.test('FCC'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FrEeCoDeCamp`。
|
||||
Your regex should match `FrEeCoDeCamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FrEeCoDeCamp'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FrEeCodECamp`。
|
||||
Your regex should match `FrEeCodECamp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FrEeCodECamp'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FReeCodeCAmp`。
|
||||
Your regex should match `FReeCodeCAmp`
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FReeCodeCAmp'));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db4367417b2b2512b90
|
||||
title: 同时用多种模式匹配文字字符串
|
||||
title: Match a Literal String with Different Possibilities
|
||||
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--
|
||||
|
||||
使用正则表达式`/coding/`,你可以在其他字符串中查找`"coding"`。
|
||||
Using regexes like `/coding/`, you can look for the pattern `"coding"` in another string.
|
||||
|
||||
这对于搜寻单个字符串非常有用,但仅限于一种匹配模式。你可以使用`|`操作符来匹配多个规则。
|
||||
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: `|`.
|
||||
|
||||
此操作符匹配操作符前面或后面的字符。例如,如果你想匹配`"yes"`或`"no"`,你需要的正则表达式是`/yes|no/`。
|
||||
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|maybe/`。
|
||||
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/`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
完成正则表达式`petRegex`以匹配`"dog"`、`"cat"`、`"bird"`或者`"fish"`。
|
||||
Complete the regex `petRegex` to match the pets `"dog"`, `"cat"`, `"bird"`, or `"fish"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
对于字符串`'John has a pet dog.'`,你的正则表达式`petRegex`的`test`方法应该返回`true`。
|
||||
Your regex `petRegex` should return `true` for the string `"John has a pet dog."`
|
||||
|
||||
```js
|
||||
assert(petRegex.test('John has a pet dog.'));
|
||||
```
|
||||
|
||||
对于字符串`'Emma has a pet rock.'`,你的正则表达式`petRegex`的`test`方法应该返回`false`。
|
||||
Your regex `petRegex` should return `false` for the string `"Emma has a pet rock."`
|
||||
|
||||
```js
|
||||
assert(!petRegex.test('Emma has a pet rock.'));
|
||||
```
|
||||
|
||||
对于字符串`'Emma has a pet bird.'`,你的正则表达式`petRegex`的`test`方法应该返回`true`。
|
||||
Your regex `petRegex` should return `true` for the string `"Emma has a pet bird."`
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Emma has a pet bird.'));
|
||||
```
|
||||
|
||||
对于字符串`'Liz has a pet cat.'`,你的正则表达式`petRegex`的`test`方法应该返回`true`。
|
||||
Your regex `petRegex` should return `true` for the string `"Liz has a pet cat."`
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Liz has a pet cat.'));
|
||||
```
|
||||
|
||||
对于字符串`'Kara has a pet dolphin.'`,你的正则表达式`petRegex`的`test`方法应该返回`false`。
|
||||
Your regex `petRegex` should return `false` for the string `"Kara has a pet dolphin."`
|
||||
|
||||
```js
|
||||
assert(!petRegex.test('Kara has a pet dolphin.'));
|
||||
```
|
||||
|
||||
对于字符串`'Alice has a pet fish.'`,你的正则表达式`petRegex`的`test`方法应该返回`true`。
|
||||
Your regex `petRegex` should return `true` for the string `"Alice has a pet fish."`
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Alice has a pet fish.'));
|
||||
```
|
||||
|
||||
对于字符串`'Jimmy has a pet computer.'`,你的正则表达式`petRegex`的`test`方法应该返回`false`。
|
||||
Your regex `petRegex` should return `false` for the string `"Jimmy has a pet computer."`
|
||||
|
||||
```js
|
||||
assert(!petRegex.test('Jimmy has a pet computer.'));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db7367417b2b2512b9f
|
||||
title: 匹配所有的字母和数字
|
||||
title: Match All Letters and Numbers
|
||||
challengeType: 1
|
||||
forumTopicId: 301346
|
||||
dashedName: match-all-letters-and-numbers
|
||||
@ -8,9 +8,9 @@ dashedName: match-all-letters-and-numbers
|
||||
|
||||
# --description--
|
||||
|
||||
使用元字符,可以使用`[a-z]`搜寻字母表中的所有字母。这种元字符是很常见的,它有一个缩写,但这个缩写也包含额外的字符。
|
||||
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.
|
||||
|
||||
JavaScript 中与字母表匹配的最接近的元字符是`\w`,这个缩写等同于`[A-Za-z0-9_]`。它不仅可以匹配大小写字母和数字,注意,它还会匹配下划线字符(`_`)。
|
||||
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 (`_`).
|
||||
|
||||
```js
|
||||
let longHand = /[A-Za-z0-9_]+/;
|
||||
@ -23,25 +23,27 @@ longHand.test(varNames); // Returns true
|
||||
shortHand.test(varNames); // Returns true
|
||||
```
|
||||
|
||||
These shortcut character classes are also known as <dfn>shorthand character classes</dfn>.
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用缩写`\w`来计算所有引号中字母和数字字符的数量。
|
||||
Use the shorthand character class `\w` to count the number of alphanumeric characters in various quotes and strings.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
Your regex should use the global flag.
|
||||
|
||||
```js
|
||||
assert(alphabetRegexV2.global);
|
||||
```
|
||||
|
||||
正则表达式应该使用元字符 `\w` 匹配字母表里的所有字符。
|
||||
Your regex should use the shorthand character `\w` to match all characters which are alphanumeric.
|
||||
|
||||
```js
|
||||
assert(/\\w/.test(alphabetRegexV2.source));
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'The five boxing wizards jump quickly.'`中匹配到 31 个字母数字字符。
|
||||
Your regex should find 31 alphanumeric characters in `"The five boxing wizards jump quickly."`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -49,7 +51,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Pack my box with five dozen liquor jugs.'`中匹配到 32 个字母数字字符。
|
||||
Your regex should find 32 alphanumeric characters in `"Pack my box with five dozen liquor jugs."`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -58,7 +60,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'How vexingly quick daft zebras jump!'`中匹配到 30 个字母数字字符。
|
||||
Your regex should find 30 alphanumeric characters in `"How vexingly quick daft zebras jump!"`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -66,7 +68,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'`中匹配到 36 个字母数字字符。
|
||||
Your regex should find 36 alphanumeric characters in `"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."`
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db8367417b2b2512ba1
|
||||
title: 匹配所有非数字
|
||||
title: Match All Non-Numbers
|
||||
challengeType: 1
|
||||
forumTopicId: 301347
|
||||
dashedName: match-all-non-numbers
|
||||
@ -8,59 +8,59 @@ dashedName: match-all-non-numbers
|
||||
|
||||
# --description--
|
||||
|
||||
上一项挑战中展示了如何使用带有小写`d`的缩写`\d`来搜寻数字。也可以使用类似的缩写来搜寻非数字,该缩写使用大写的`D`。
|
||||
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`。这等同于字符串`[^0-9]`,它查找不是 0 - 9 之间数字的单个字符。
|
||||
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.
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用非数字缩写`\D`来计算电影标题中有多少非数字。
|
||||
Use the shorthand character class for non-digits `\D` to count how many non-digits are in movie titles.
|
||||
|
||||
# --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);
|
||||
```
|
||||
|
||||
你的正则表达式在`'9'`中应该匹配不到非数字。
|
||||
Your regex should find no non-digits in `"9"`.
|
||||
|
||||
```js
|
||||
assert('9'.match(noNumRegex) == null);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Catch 22'`中匹配到 6 个非数字。
|
||||
Your regex should find 6 non-digits in `"Catch 22"`.
|
||||
|
||||
```js
|
||||
assert('Catch 22'.match(noNumRegex).length == 6);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'101 Dalmatians'`中匹配到 11 个非数字。
|
||||
Your regex should find 11 non-digits in `"101 Dalmatians"`.
|
||||
|
||||
```js
|
||||
assert('101 Dalmatians'.match(noNumRegex).length == 11);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'One, Two, Three'`中匹配到 15 个非数字。
|
||||
Your regex should find 15 non-digits in `"One, Two, Three"`.
|
||||
|
||||
```js
|
||||
assert('One, Two, Three'.match(noNumRegex).length == 15);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'21 Jump Street'`中匹配到 12 个非数字。
|
||||
Your regex should find 12 non-digits in `"21 Jump Street"`.
|
||||
|
||||
```js
|
||||
assert('21 Jump Street'.match(noNumRegex).length == 12);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`"2001: A Space Odyssey"`中匹配到 17 个非数字。
|
||||
Your regex should find 17 non-digits in `"2001: A Space Odyssey"`.
|
||||
|
||||
```js
|
||||
assert('2001: A Space Odyssey'.match(noNumRegex).length == 17);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 5d712346c441eddfaeb5bdef
|
||||
title: 匹配所有数字
|
||||
title: Match All Numbers
|
||||
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.
|
||||
|
||||
查找数字字符的缩写是`\d`,注意是小写的`d`。这等同于元字符`[0-9]`,它查找 0 到 9 之间任意数字的单个字符。
|
||||
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.
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用缩写`\d`来计算电影标题中有多少个数字。书面数字("six" 而不是 6)不计算在内。
|
||||
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.
|
||||
|
||||
# --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);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'9'`中匹配到 1 个数字。
|
||||
Your regex should find 1 digit in `"9"`.
|
||||
|
||||
```js
|
||||
assert('9'.match(numRegex).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Catch 22'`中匹配到 2 个数字。
|
||||
Your regex should find 2 digits in `"Catch 22"`.
|
||||
|
||||
```js
|
||||
assert('Catch 22'.match(numRegex).length == 2);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'101 Dalmatians'`中匹配到 3 个数字。
|
||||
Your regex should find 3 digits in `"101 Dalmatians"`.
|
||||
|
||||
```js
|
||||
assert('101 Dalmatians'.match(numRegex).length == 3);
|
||||
```
|
||||
|
||||
你的正则表达式在`'One, Two, Three'`中应该匹配不到数字。
|
||||
Your regex should find no digits in `"One, Two, Three"`.
|
||||
|
||||
```js
|
||||
assert('One, Two, Three'.match(numRegex) == null);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'21 Jump Street'`中匹配到 2 个数字。
|
||||
Your regex should find 2 digits in `"21 Jump Street"`.
|
||||
|
||||
```js
|
||||
assert('21 Jump Street'.match(numRegex).length == 2);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`"2001: A Space Odyssey"`中匹配到 4 个数字。
|
||||
Your regex should find 4 digits in `"2001: A Space Odyssey"`.
|
||||
|
||||
```js
|
||||
assert('2001: A Space Odyssey'.match(numRegex).length == 4);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db5367417b2b2512b94
|
||||
title: 用通配符.匹配任何内容
|
||||
title: Match Anything with Wildcard Period
|
||||
challengeType: 1
|
||||
forumTopicId: 301348
|
||||
dashedName: match-anything-with-wildcard-period
|
||||
@ -8,9 +8,9 @@ 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: `.`
|
||||
|
||||
通配符`.`将匹配任何一个字符。通配符也叫`dot`或`period`。可以像使用正则表达式中任何其他字符一样使用通配符。例如,如果想匹配`"hug"`、`"huh"`、`"hut"`和`"hum"`,可以使用正则表达式`/hu./`匹配以上四个单词。
|
||||
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.
|
||||
|
||||
```js
|
||||
let humStr = "I'll hum a song";
|
||||
@ -22,55 +22,61 @@ huRegex.test(hugStr); // Returns true
|
||||
|
||||
# --instructions--
|
||||
|
||||
完成正则表达式`unRegex`以匹配字符串`"run"`、`"sun"`、`"fun"`、`"pun"`、`"nun"`和`"bun"`。正则表达式中应该使用通配符。
|
||||
Complete the regex `unRegex` so that it matches the strings `"run"`, `"sun"`, `"fun"`, `"pun"`, `"nun"`, and `"bun"`. Your regex should use the wildcard character.
|
||||
|
||||
# --hints--
|
||||
|
||||
你应该使用`.test()`方法。
|
||||
You should use the `.test()` method.
|
||||
|
||||
```js
|
||||
assert(code.match(/\.test\(.*\)/));
|
||||
```
|
||||
|
||||
你应该在你的正则表达式`unRegex`中使用通配符。
|
||||
You should use the wildcard character in your regex `unRegex`
|
||||
|
||||
```js
|
||||
assert(/\./.test(unRegex.source));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'Let us go on a run.'`中匹配到`'run'`单词。
|
||||
Your regex `unRegex` should match `"run"` in `"Let us go on a run."`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('Let us go on a run.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'The sun is out today.'`中匹配到`'sun'`单词。
|
||||
Your regex `unRegex` should match `"sun"` in `"The sun is out today."`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('The sun is out today.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'Coding is a lot of fun.'`中匹配到`'fun'`单词。
|
||||
Your regex `unRegex` should match `"fun"` in `"Coding is a lot of fun."`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('Coding is a lot of fun.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'Seven days without a pun makes one weak.'`中匹配到`'pun'`单词。
|
||||
Your regex `unRegex` should match `"pun"` in `"Seven days without a pun makes one weak."`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('Seven days without a pun makes one weak.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'One takes a vow to be a nun.'`中匹配到`'nun'`单词。
|
||||
Your regex `unRegex` should match `"nun"` in `"One takes a vow to be a nun."`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(unRegex.test('One takes a vow to be a nun.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'She got fired from the hot dog stand for putting her hair in a bun.'`中匹配到`'bun'`单词。
|
||||
Your regex `unRegex` should match `"bun"` in `"She got fired from the hot dog stand for putting her hair in a bun."`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(
|
||||
unRegex.test(
|
||||
'She got fired from the hot dog stand for putting her hair in a bun.'
|
||||
@ -78,16 +84,18 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`不应该匹配`'There is a bug in my code.'`。
|
||||
Your regex `unRegex` should not match `"There is a bug in my code."`
|
||||
|
||||
```js
|
||||
unRegex.lastIndex = 0;
|
||||
assert(!unRegex.test('There is a bug in my code.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`不应该匹配`'Catch me if you can.'`。
|
||||
Your regex `unRegex` should not match `"Catch me if you can."`
|
||||
|
||||
```js
|
||||
assert(!unRegex.test('Can me if you can.'));
|
||||
unRegex.lastIndex = 0;
|
||||
assert(!unRegex.test('Catch me if you can.'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db7367417b2b2512b9d
|
||||
title: 匹配字符串的开头
|
||||
title: Match Beginning String Patterns
|
||||
challengeType: 1
|
||||
forumTopicId: 301349
|
||||
dashedName: match-beginning-string-patterns
|
||||
@ -8,9 +8,9 @@ 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.
|
||||
|
||||
在之前的挑战中,使用`字符集`中的`插入`符号(`^`)来创建一个`否定字符集`,形如`[^thingsThatWillNotBeMatched]`。在`字符集`之外,`插入`符号用于字符串的开头搜寻匹配模式。
|
||||
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.
|
||||
|
||||
```js
|
||||
let firstString = "Ricky is first and can be found.";
|
||||
@ -24,29 +24,29 @@ firstRegex.test(notFirst);
|
||||
|
||||
# --instructions--
|
||||
|
||||
在正则表达式中使用`^`符号,以匹配仅在字符串`rickyAndCal`的开头出现的`"Cal"`。
|
||||
Use the caret character in a regex to find `"Cal"` only in the beginning of the string `rickyAndCal`.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该搜寻有一个大写字母的`'Cal'`。
|
||||
Your regex should search for `"Cal"` with a capital letter.
|
||||
|
||||
```js
|
||||
assert(calRegex.source == '^Cal');
|
||||
```
|
||||
|
||||
你的正则表达式不应该使用任何标志。
|
||||
Your regex should not use any flags.
|
||||
|
||||
```js
|
||||
assert(calRegex.flags == '');
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配字符串开头的`'Cal'`。
|
||||
Your regex should match `"Cal"` at the beginning of the string.
|
||||
|
||||
```js
|
||||
assert(calRegex.test('Cal and Ricky both like racing.'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配字符串中间的`'Cal'`。
|
||||
Your regex should not match `"Cal"` in the middle of a string.
|
||||
|
||||
```js
|
||||
assert(!calRegex.test('Ricky and Cal both like racing.'));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db6367417b2b2512b99
|
||||
title: 匹配出现一次或多次的字符
|
||||
title: Match Characters that Occur One or More Times
|
||||
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.
|
||||
|
||||
例如,`/a+/g`会在`"abc"`中匹配到一个匹配项,并且返回`["a"]`。因为`+`的存在,它也会在`"aabc"`中匹配到一个匹配项,然后返回`["aa"]`。
|
||||
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"]`.
|
||||
|
||||
如果它是检查字符串`"abab"`,它将匹配到两个匹配项并且返回`["a", "a"]`,因为`a`字符不连续,在它们之间有一个`b`字符。最后,因为在字符串`"bcd"`中没有`"a"`,因此找不到匹配项。
|
||||
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.
|
||||
|
||||
# --instructions--
|
||||
|
||||
想要在字符串`"Mississippi"`中匹配到出现一次或多次的字母`s`的匹配项。编写一个使用`+`符号的正则表达式。
|
||||
You want to find matches when the letter `s` occurs one or more times in `"Mississippi"`. Write a regex that uses the `+` sign.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式`myRegex`应该使用`+`符号来匹配一个或多个`s`字符。
|
||||
Your regex `myRegex` should use the `+` sign to match one or more `s` characters.
|
||||
|
||||
```js
|
||||
assert(/\+/.test(myRegex.source));
|
||||
```
|
||||
|
||||
你的正则表达式`myRegex`应该匹配两项。
|
||||
Your regex `myRegex` should match 2 items.
|
||||
|
||||
```js
|
||||
assert(result.length == 2);
|
||||
```
|
||||
|
||||
`结果`变量应该是一个包含两个`'ss'`匹配项的数组。
|
||||
The `result` variable should be an array with two matches of `"ss"`
|
||||
|
||||
```js
|
||||
assert(result[0] == 'ss' && result[1] == 'ss');
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db6367417b2b2512b9a
|
||||
title: 匹配出现零次或多次的字符
|
||||
title: Match Characters that Occur Zero or More Times
|
||||
challengeType: 1
|
||||
forumTopicId: 301351
|
||||
dashedName: match-characters-that-occur-zero-or-more-times
|
||||
@ -8,9 +8,9 @@ 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.
|
||||
|
||||
执行该操作的字符叫做`asterisk`或`star`,即`*`。
|
||||
The character to do this is the asterisk or star: `*`.
|
||||
|
||||
```js
|
||||
let soccerWord = "gooooooooal!";
|
||||
@ -24,35 +24,35 @@ oPhrase.match(goRegex); // Returns null
|
||||
|
||||
# --instructions--
|
||||
|
||||
在这个挑战里,`chewieQuote` 已经被初始化为 "Aaaaaaaaaaaaaaaarrrgh!"。创建一个变量为`chewieRegex`的正则表达式,使用`*`符号在`chewieQuote`中匹配`"A"`及其之后出现的零个或多个`"a"`。你的正则表达式不需要使用修饰符,也不需要匹配引号。
|
||||
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.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式`chewieRegex`应该使用`*`符号匹配`'A'`之后出现的零个或多个`'a'`字符。
|
||||
Your regex `chewieRegex` should use the `*` character to match zero or more `a` characters.
|
||||
|
||||
```js
|
||||
assert(/\*/.test(chewieRegex.source));
|
||||
```
|
||||
|
||||
正则表达式应当匹配 `chewieQuote` 里的 `"A"`。
|
||||
Your regex should match `"A"` in `chewieQuote`.
|
||||
|
||||
```js
|
||||
assert(result[0][0] === 'A');
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Aaaaaaaaaaaaaaaa'`。
|
||||
Your regex should match `"Aaaaaaaaaaaaaaaa"` in `chewieQuote`.
|
||||
|
||||
```js
|
||||
assert(result[0] === 'Aaaaaaaaaaaaaaaa');
|
||||
```
|
||||
|
||||
你的正则表达式`chewieRegex`应该匹配 16 个字符。
|
||||
Your regex `chewieRegex` should match 16 characters in `chewieQuote`.
|
||||
|
||||
```js
|
||||
assert(result[0].length === 16);
|
||||
```
|
||||
|
||||
你的正则表达式在`'He made a fair move. Screaming about it can't help you.'`中不应该匹配任何字符。
|
||||
Your regex should not match any characters in "He made a fair move. Screaming about it can't help you."
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -60,7 +60,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式在`'Let him have it. It's not wise to upset a Wookiee.'`中不应该匹配任何字符。
|
||||
Your regex should not match any characters in "Let him have it. It's not wise to upset a Wookiee."
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db7367417b2b2512b9e
|
||||
title: 匹配字符串的末尾
|
||||
title: Match Ending String Patterns
|
||||
challengeType: 1
|
||||
forumTopicId: 301352
|
||||
dashedName: match-ending-string-patterns
|
||||
@ -8,9 +8,9 @@ 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";
|
||||
@ -25,23 +25,23 @@ storyRegex.test(noEnding);
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用`$`在字符串`caboose`的末尾匹配`"caboose"`。
|
||||
Use the anchor character (`$`) to match the string `"caboose"` at the end of the string `caboose`.
|
||||
|
||||
# --hints--
|
||||
|
||||
你应该在正则表达式使用美元符号`$`来搜寻`'caboose'`。
|
||||
You should search for `"caboose"` with the dollar sign `$` anchor in your regex.
|
||||
|
||||
```js
|
||||
assert(lastRegex.source == 'caboose$');
|
||||
```
|
||||
|
||||
你的正则表达式不应该使用任何标志。
|
||||
Your regex should not use any flags.
|
||||
|
||||
```js
|
||||
assert(lastRegex.flags == '');
|
||||
```
|
||||
|
||||
你应该在字符串`'The last car on a train is the caboose'`的末尾匹配`'caboose'`。
|
||||
You should match `"caboose"` at the end of the string `"The last car on a train is the caboose"`
|
||||
|
||||
```js
|
||||
assert(lastRegex.test('The last car on a train is the caboose'));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db8367417b2b2512ba0
|
||||
title: 匹配除了字母和数字的所有符号
|
||||
title: Match Everything But Letters and Numbers
|
||||
challengeType: 1
|
||||
forumTopicId: 301353
|
||||
dashedName: match-everything-but-letters-and-numbers
|
||||
@ -8,9 +8,9 @@ dashedName: match-everything-but-letters-and-numbers
|
||||
|
||||
# --description--
|
||||
|
||||
已经了解到可以使用缩写`\w`来匹配字母和数字`[A-Za-z0-9_]`。不过,有可能想要搜寻的匹配模式是非字母数字字符。
|
||||
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`搜寻和`\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_]`.
|
||||
|
||||
```js
|
||||
let shortHand = /\W/;
|
||||
@ -22,17 +22,17 @@ sentence.match(shortHand); // Returns ["!"]
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用缩写`\W`来计算不同引号和字符串中非字母数字字符的数量。
|
||||
Use the shorthand character class `\W` to count the number of non-alphanumeric characters in various quotes and strings.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
Your regex should use the global flag.
|
||||
|
||||
```js
|
||||
assert(nonAlphabetRegex.global);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'The five boxing wizards jump quickly.'`中匹配到 6 个非字母数字字符。
|
||||
Your regex should find 6 non-alphanumeric characters in `"The five boxing wizards jump quickly."`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -40,13 +40,13 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
正则表达式应该使用元字符来匹配非字母字符。
|
||||
Your regex should use the shorthand character to match characters which are non-alphanumeric.
|
||||
|
||||
```js
|
||||
assert(/\\W/.test(nonAlphabetRegex.source));
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Pack my box with five dozen liquor jugs.'`中匹配到 8 个非字母数字字符。
|
||||
Your regex should find 8 non-alphanumeric characters in `"Pack my box with five dozen liquor jugs."`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -54,7 +54,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'How vexingly quick daft zebras jump!'`中匹配到 6 个非字母数字字符。
|
||||
Your regex should find 6 non-alphanumeric characters in `"How vexingly quick daft zebras jump!"`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -62,7 +62,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'`中匹配到 12 个非字母数字字符。
|
||||
Your regex should find 12 non-alphanumeric characters in `"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."`
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db5367417b2b2512b96
|
||||
title: 匹配字母表中的字母
|
||||
title: Match Letters of the Alphabet
|
||||
challengeType: 1
|
||||
forumTopicId: 301354
|
||||
dashedName: match-letters-of-the-alphabet
|
||||
@ -8,11 +8,11 @@ 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.
|
||||
|
||||
在`字符集`中,可以使用`连字符`(`-`)来定义要匹配的字符范围。
|
||||
Inside a character set, you can define a range of characters to match using a hyphen character: `-`.
|
||||
|
||||
例如,要匹配小写字母`a`到`e`,你可以使用`[a-e]`。
|
||||
For example, to match lowercase letters `a` through `e` you would use `[a-e]`.
|
||||
|
||||
```js
|
||||
let catStr = "cat";
|
||||
@ -26,26 +26,25 @@ matStr.match(bgRegex); // Returns null
|
||||
|
||||
# --instructions--
|
||||
|
||||
匹配字符串`quoteSample`中的所有字母。
|
||||
Match all the letters in the string `quoteSample`.
|
||||
|
||||
**注意:**
|
||||
一定要同时匹配大小写**字母**。\*\*\*\*
|
||||
**Note**: Be sure to match both uppercase and lowercase letters.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式`alphabetRegex`应该匹配 35 项。
|
||||
Your regex `alphabetRegex` should match 35 items.
|
||||
|
||||
```js
|
||||
assert(result.length == 35);
|
||||
```
|
||||
|
||||
你的正则表达式`alphabetRegex`应该使用全局标志。
|
||||
Your regex `alphabetRegex` should use the global flag.
|
||||
|
||||
```js
|
||||
assert(alphabetRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式`alphabetRegex`应该使用忽略大小写标志。
|
||||
Your regex `alphabetRegex` should use the case insensitive flag.
|
||||
|
||||
```js
|
||||
assert(alphabetRegex.flags.match(/i/).length == 1);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db3367417b2b2512b8f
|
||||
title: 匹配文字字符串
|
||||
title: Match Literal Strings
|
||||
challengeType: 1
|
||||
forumTopicId: 301355
|
||||
dashedName: match-literal-strings
|
||||
@ -8,7 +8,7 @@ dashedName: match-literal-strings
|
||||
|
||||
# --description--
|
||||
|
||||
在上一个挑战中,使用正则表达式`/Hello/`搜索到了字符串`"Hello"`。那个正则表达式在字符串中搜寻`"Hello"`的文字匹配。下面是另一个在字符串中搜寻`"Kevin"`的示例:
|
||||
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"`:
|
||||
|
||||
```js
|
||||
let testStr = "Hello, my name is Kevin.";
|
||||
@ -17,7 +17,7 @@ testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
任何其他形式的`"Kevin"`都不会被匹配。例如,正则表达式`/Kevin/`不会匹配`"kevin"`或者`"KEVIN"`。
|
||||
Any other forms of `"Kevin"` will not match. For example, the regex `/Kevin/` will not match `"kevin"` or `"KEVIN"`.
|
||||
|
||||
```js
|
||||
let wrongRegex = /kevin/;
|
||||
@ -25,27 +25,27 @@ wrongRegex.test(testStr);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
后续的挑战将为你展示如何匹配其他形式的字符串。
|
||||
A future challenge will show how to match those other forms as well.
|
||||
|
||||
# --instructions--
|
||||
|
||||
完成正则表达式`waldoRegex`,在字符串`waldoIsHiding`中匹配到文本`"Waldo"`。
|
||||
Complete the regex `waldoRegex` to find `"Waldo"` in the string `waldoIsHiding` with a literal match.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式`waldoRegex`应该匹配到`'Waldo'`。
|
||||
Your regex `waldoRegex` should find `"Waldo"`
|
||||
|
||||
```js
|
||||
assert(waldoRegex.test(waldoIsHiding));
|
||||
```
|
||||
|
||||
你的正则表达式`waldoRegex`不应该搜寻其他的任何内容。
|
||||
Your regex `waldoRegex` should not search for anything else.
|
||||
|
||||
```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));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db9367417b2b2512ba4
|
||||
title: 匹配非空白字符
|
||||
title: Match Non-Whitespace Characters
|
||||
challengeType: 1
|
||||
forumTopicId: 18210
|
||||
dashedName: match-non-whitespace-characters
|
||||
@ -8,9 +8,9 @@ dashedName: match-non-whitespace-characters
|
||||
|
||||
# --description--
|
||||
|
||||
已经学会了如何使用带有小写`s`的缩写`\s`来搜寻空白字符。还可以搜寻除了空格之外的所有内容。
|
||||
You learned about searching for whitespace using `\s`, with a lowercase `s`. You can also search for everything except whitespace.
|
||||
|
||||
使用`\S`搜寻非空白字符,其中`S`是大写。此匹配模式将不匹配空格、回车符、制表符、换页符和换行符。可以认为这类似于元字符`[^\r\t\f\n\v]`。
|
||||
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]`.
|
||||
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
@ -20,23 +20,23 @@ whiteSpace.match(nonSpaceRegex).length; // Returns 32
|
||||
|
||||
# --instructions--
|
||||
|
||||
修改正则表达式`countNonWhiteSpace`以查找字符串中的多个非空字符。
|
||||
Change the regex `countNonWhiteSpace` to look for multiple non-whitespace characters in a string.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
Your regex should use the global flag.
|
||||
|
||||
```js
|
||||
assert(countNonWhiteSpace.global);
|
||||
```
|
||||
|
||||
正则表达式应该使用元字符 <code>\\S/code> 来匹配所有的非空格字符。
|
||||
Your regex should use the shorthand character `\S` to match all non-whitespace characters.
|
||||
|
||||
```js
|
||||
assert(/\\S/.test(countNonWhiteSpace.source));
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Men are from Mars and women are from Venus.'`中匹配到 35 个非空白字符。
|
||||
Your regex should find 35 non-spaces in `"Men are from Mars and women are from Venus."`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -45,13 +45,13 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`"Space: the final frontier."`中匹配到 23 个非空白字符。
|
||||
Your regex should find 23 non-spaces in `"Space: the final frontier."`
|
||||
|
||||
```js
|
||||
assert('Space: the final frontier.'.match(countNonWhiteSpace).length == 23);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'MindYourPersonalSpace'`中匹配到 21 个非空白字符。
|
||||
Your regex should find 21 non-spaces in `"MindYourPersonalSpace"`
|
||||
|
||||
```js
|
||||
assert('MindYourPersonalSpace'.match(countNonWhiteSpace).length == 21);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db5367417b2b2512b97
|
||||
title: 匹配字母表中的数字和字母
|
||||
title: Match Numbers and Letters of the Alphabet
|
||||
challengeType: 1
|
||||
forumTopicId: 301356
|
||||
dashedName: match-numbers-and-letters-of-the-alphabet
|
||||
@ -8,11 +8,11 @@ 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.
|
||||
|
||||
例如,`/[0-5]/`匹配`0`和`5`之间的任意数字,包含`0`和`5`。
|
||||
For example, `/[0-5]/` matches any number between `0` and `5`, including the `0` and `5`.
|
||||
|
||||
此外,还可以在单个字符集中组合一系列字母和数字。
|
||||
Also, it is possible to combine a range of letters and numbers in a single character set.
|
||||
|
||||
```js
|
||||
let jennyStr = "Jenny8675309";
|
||||
@ -23,23 +23,23 @@ jennyStr.match(myRegex);
|
||||
|
||||
# --instructions--
|
||||
|
||||
创建一个正则表达式,使其可以匹配`h`和`s`之间的一系列字母,以及`2`和`6`之间的一系列数字。请记得在正则表达式中包含恰当的标志。
|
||||
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.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式`myRegex`应该匹配 17 项。
|
||||
Your regex `myRegex` should match 17 items.
|
||||
|
||||
```js
|
||||
assert(result.length == 17);
|
||||
```
|
||||
|
||||
你的正则表达式`myRegex`应该使用全局标志。
|
||||
Your regex `myRegex` should use the global flag.
|
||||
|
||||
```js
|
||||
assert(myRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式`myRegex`应该使用忽略大小写的标志。
|
||||
Your regex `myRegex` should use the case insensitive flag.
|
||||
|
||||
```js
|
||||
assert(myRegex.flags.match(/i/).length == 1);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db5367417b2b2512b95
|
||||
title: 将单个字符与多种可能性匹配
|
||||
title: Match Single Character with Multiple Possibilities
|
||||
challengeType: 1
|
||||
forumTopicId: 301357
|
||||
dashedName: match-single-character-with-multiple-possibilities
|
||||
@ -8,11 +8,11 @@ dashedName: match-single-character-with-multiple-possibilities
|
||||
|
||||
# --description--
|
||||
|
||||
已经了解了文字匹配模式(`/literal/`)和通配符(`/./`)。这是正则表达式的两种极端情况,一种是精确匹配,而另一种则是匹配所有。在这两种极端情况之间有一个平衡选项。
|
||||
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.
|
||||
|
||||
可以使用`字符集`搜寻具有一定灵活性的文字匹配模式。可以把字符集放在方括号(`[`和`]`)之间来定义一组需要匹配的字符串。
|
||||
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.
|
||||
|
||||
例如,如果想要匹配`"bag"`、`"big"`和`"bug"`,但是不想匹配`"bog"`。可以创建正则表达式`/b[aiu]g/`来执行此操作。`[aiu]`是只匹配字符`"a"`、`"i"`或者`"u"`的字符集。
|
||||
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"`.
|
||||
|
||||
```js
|
||||
let bigStr = "big";
|
||||
@ -28,38 +28,38 @@ bogStr.match(bgRegex); // Returns null
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用元音字符集(`a`、`e`、`i`、`o`、`u`)在正则表达式`vowelRegex`中匹配到字符串`quoteSample`中的所有元音。
|
||||
Use a character class with vowels (`a`, `e`, `i`, `o`, `u`) in your regex `vowelRegex` to find all the vowels in the string `quoteSample`.
|
||||
|
||||
**注意**
|
||||
一定要同时匹配大小写元音。
|
||||
**Note**
|
||||
Be sure to match both upper- and lowercase vowels.
|
||||
|
||||
# --hints--
|
||||
|
||||
你应该匹配到所有25个元音。
|
||||
You should find all 25 vowels.
|
||||
|
||||
```js
|
||||
assert(result.length == 25);
|
||||
```
|
||||
|
||||
你的正则表达式`vowelRegex`应该使用字符集。
|
||||
Your regex `vowelRegex` should use a character class.
|
||||
|
||||
```js
|
||||
assert(/\[.*\]/.test(vowelRegex.source));
|
||||
```
|
||||
|
||||
你的正则表达式`vowelRegex`应该使用全局标志。
|
||||
Your regex `vowelRegex` should use the global flag.
|
||||
|
||||
```js
|
||||
assert(vowelRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式`vowelRegex`应该使用忽略大小写标志。
|
||||
Your regex `vowelRegex` should use the case insensitive flag.
|
||||
|
||||
```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()));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db6367417b2b2512b98
|
||||
title: 匹配单个未指定的字符
|
||||
title: Match Single Characters Not Specified
|
||||
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>.
|
||||
|
||||
要创建`否定字符集`,需要在开始括号后面和不想匹配的字符前面放置`插入字符`(即`^`)。
|
||||
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.
|
||||
|
||||
例如,`/[^aeiou]/gi`匹配所有非元音字符。注意,字符`.`、`!`、`[`、`@`、`/`和空白字符等也会被匹配,该否定字符集仅排除元音字符。
|
||||
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.
|
||||
|
||||
# --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--
|
||||
|
||||
你的正则表达式`myRegex`应该匹配 9 项。
|
||||
Your regex `myRegex` should match 9 items.
|
||||
|
||||
```js
|
||||
assert(result.length == 9);
|
||||
```
|
||||
|
||||
你的正则表达式`myRegex`应该使用全局标志。
|
||||
Your regex `myRegex` should use the global flag.
|
||||
|
||||
```js
|
||||
assert(myRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式`myRegex`应该使用忽略大小写标志。
|
||||
Your regex `myRegex` should use the case insensitive flag.
|
||||
|
||||
```js
|
||||
assert(myRegex.flags.match(/i/).length == 1);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db8367417b2b2512ba3
|
||||
title: 匹配空白字符
|
||||
title: Match Whitespace
|
||||
challengeType: 1
|
||||
forumTopicId: 301359
|
||||
dashedName: match-whitespace
|
||||
@ -8,9 +8,9 @@ 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.
|
||||
|
||||
可以使用`\s`搜寻空格,其中`s`是小写。此匹配模式不仅匹配空格,还匹配回车符、制表符、换页符和换行符,可以将其视为与`[\r\t\f\n\v]`类似。
|
||||
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]`.
|
||||
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
@ -21,23 +21,23 @@ whiteSpace.match(spaceRegex);
|
||||
|
||||
# --instructions--
|
||||
|
||||
修改正则表达式`countWhiteSpace`查找字符串中的多个空白字符。
|
||||
Change the regex `countWhiteSpace` to look for multiple whitespace characters in a string.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
Your regex should use the global flag.
|
||||
|
||||
```js
|
||||
assert(countWhiteSpace.global);
|
||||
```
|
||||
|
||||
正则表达式应该使用元字符 `\s` 匹配所有的空白。
|
||||
Your regex should use the shorthand character `\s` to match all whitespace characters.
|
||||
|
||||
```js
|
||||
assert(/\\s/.test(countWhiteSpace.source));
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Men are from Mars and women are from Venus.'`中匹配到 8 个空白字符。
|
||||
Your regex should find eight spaces in `"Men are from Mars and women are from Venus."`
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -46,13 +46,13 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`"Space: the final frontier."`中匹配到 3 个空白字符。
|
||||
Your regex should find three spaces in `"Space: the final frontier."`
|
||||
|
||||
```js
|
||||
assert('Space: the final frontier.'.match(countWhiteSpace).length == 3);
|
||||
```
|
||||
|
||||
你的正则表达式在`'MindYourPersonalSpace'`中应该匹配不到空白字符。
|
||||
Your regex should find no spaces in `"MindYourPersonalSpace"`
|
||||
|
||||
```js
|
||||
assert('MindYourPersonalSpace'.match(countWhiteSpace) == null);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7dba367417b2b2512ba9
|
||||
title: 正向先行断言和负向先行断言
|
||||
title: Positive and Negative Lookahead
|
||||
challengeType: 1
|
||||
forumTopicId: 301360
|
||||
dashedName: positive-and-negative-lookahead
|
||||
@ -8,15 +8,15 @@ dashedName: positive-and-negative-lookahead
|
||||
|
||||
# --description--
|
||||
|
||||
`先行断言`是告诉 JavaScript 在字符串中向前查找的匹配模式。当想要在同一个字符串上搜寻多个匹配模式时,这可能非常有用。
|
||||
<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.
|
||||
|
||||
有两种`先行断言`:`正向先行断言`和`负向先行断言`。
|
||||
There are two kinds of lookaheads: <dfn>positive lookahead</dfn> and <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";
|
||||
@ -27,7 +27,7 @@ quit.match(quRegex); // Returns ["q"]
|
||||
noquit.match(qRegex); // Returns ["q"]
|
||||
```
|
||||
|
||||
`先行断言`的更实际用途是检查一个字符串中的两个或更多匹配模式。这里有一个简单的密码检查器,密码规则是 3 到 6 个字符且至少包含一个数字:
|
||||
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:
|
||||
|
||||
```js
|
||||
let password = "abc123";
|
||||
@ -37,68 +37,62 @@ checkPass.test(password); // Returns true
|
||||
|
||||
# --instructions--
|
||||
|
||||
在正则表达式`pwRegex`中使用`先行断言`以匹配大于5个字符且有两个连续数字的密码,并且不能以数字开头。
|
||||
Use lookaheads in the `pwRegex` to match passwords that are greater than 5 characters long, and have two consecutive digits.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用两个正向`先行断言`。
|
||||
Your regex should use two positive `lookaheads`.
|
||||
|
||||
```js
|
||||
assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'astronaut'`。
|
||||
Your regex should not match `"astronaut"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('astronaut'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'airplanes'`。
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('airplanes'));
|
||||
```
|
||||
|
||||
正则不应该匹配 `"banan1"`
|
||||
Your regex should not match `"banan1"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('banan1'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'bana12'`。
|
||||
Your regex should match `"bana12"`
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('bana12'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'abc123'`。
|
||||
Your regex should match `"abc123"`
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('abc123'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'123'`。
|
||||
Your regex should not match `"12345"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('123'));
|
||||
assert(!pwRegex.test('12345'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'1234'`。
|
||||
Your regex should match `"8pass99"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('1234'));
|
||||
assert(pwRegex.test('8pass99'));
|
||||
```
|
||||
|
||||
正则不应该匹配 `"8pass99"`
|
||||
Your regex should not match `"1a2bcde"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('8pass99'));
|
||||
assert(!pwRegex.test('1a2bcde'));
|
||||
```
|
||||
|
||||
正则不应该匹配 `"12abcde"`
|
||||
Your regex should match `"astr1on11aut"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('12abcde'));
|
||||
assert(pwRegex.test('astr1on11aut'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@ -114,5 +108,5 @@ let result = pwRegex.test(sampleWord);
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var pwRegex = /^\D(?=\w{5})(?=\w*\d{2})/;
|
||||
let pwRegex = /(?=\w{6})(?=\w*\d{2})/;
|
||||
```
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7dbb367417b2b2512bac
|
||||
title: 删除开头和结尾的空白
|
||||
title: Remove Whitespace from Start and End
|
||||
challengeType: 1
|
||||
forumTopicId: 301362
|
||||
dashedName: remove-whitespace-from-start-and-end
|
||||
@ -8,30 +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.
|
||||
|
||||
**注意:**
|
||||
`.trim()`方法在这里也可以实现同样的效果,但是你需要使用正则表达式来完成此项挑战。
|
||||
**Note:** The `String.prototype.trim()` method would work here, but you'll need to complete this challenge using regular expressions.
|
||||
|
||||
# --hints--
|
||||
|
||||
`结果`应该等于`'Hello, World!'`。
|
||||
`result` should equal to `"Hello, World!"`
|
||||
|
||||
```js
|
||||
assert(result == 'Hello, World!');
|
||||
```
|
||||
|
||||
你不应该使用`.trim()`方法。
|
||||
Your solution should not use the `String.prototype.trim()` method.
|
||||
|
||||
```js
|
||||
assert(!code.match(/\.trim\(.*?\)/));
|
||||
assert(!code.match(/\.?[\s\S]*?trim/));
|
||||
```
|
||||
|
||||
`结果`变量不应该设置为等于字符串。
|
||||
The `result` variable should not be set equal to a string.
|
||||
|
||||
```js
|
||||
assert(!code.match(/result\s*=\s*".*?"/));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db8367417b2b2512ba2
|
||||
title: 限制可能的用户名
|
||||
title: Restrict Possible Usernames
|
||||
challengeType: 1
|
||||
forumTopicId: 301363
|
||||
dashedName: restrict-possible-usernames
|
||||
@ -8,90 +8,96 @@ 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) 用户名只能是数字字母字符。
|
||||
1) Usernames can only use alpha-numeric characters.
|
||||
|
||||
2) 用户名中的数字必须在最后,且数字可以有零个或多个。
|
||||
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.
|
||||
|
||||
3) 用户名字母可以是小写字母和大写字母。
|
||||
3) Username letters can be lowercase and uppercase.
|
||||
|
||||
4) 用户名长度必须至少为两个字符。两位用户名只能使用字母。
|
||||
4) Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
|
||||
|
||||
# --instructions--
|
||||
|
||||
修改正则表达式`userCheck`以适合上面列出的约束。
|
||||
Change the regex `userCheck` to fit the constraints listed above.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该匹配`JACK`。
|
||||
Your regex should match `JACK`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('JACK'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`J`。
|
||||
Your regex should not match `J`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('J'));
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `Jo`。
|
||||
Your regex should match `Jo`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Jo'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`Oceans11`。
|
||||
Your regex should match `Oceans11`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Oceans11'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`RegexGuru`。
|
||||
Your regex should match `RegexGuru`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('RegexGuru'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`007`。
|
||||
Your regex should not match `007`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('007'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`9`。
|
||||
Your regex should not match `9`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('9'));
|
||||
```
|
||||
|
||||
正则表达式不应该匹配 `A1`。
|
||||
Your regex should not match `A1`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('A1'));
|
||||
```
|
||||
|
||||
正则表达式不应该匹配 `BadUs3rnam3`。
|
||||
Your regex should not match `BadUs3rnam3`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('BadUs3rnam3'));
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `Z97`。
|
||||
Your regex should match `Z97`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Z97'));
|
||||
```
|
||||
|
||||
正则表达式不应该匹配 `c57bT3`。
|
||||
Your regex should not match `c57bT3`
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('c57bT3'));
|
||||
```
|
||||
|
||||
Your regex should match `AB1`
|
||||
|
||||
```js
|
||||
assert(userCheck.test('AB1'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
@ -27,7 +27,7 @@ repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
|
||||
|
||||
# --instructions--
|
||||
|
||||
在正则表达式 `reRegex` 中使用捕获组,以匹配在字符串中被空格隔开的三个相同数字。
|
||||
Use capture groups in `reRegex` to match a string that consists of only the same number repeated exactly three times separated by single spaces.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -43,52 +43,43 @@ assert(reRegex.source.match(/\\d/));
|
||||
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
||||
```
|
||||
|
||||
你的正则表达式应该有两个空格分隔三个数字。
|
||||
|
||||
```js
|
||||
assert(
|
||||
reRegex.source.match(/ |\\s/g).length === 2 ||
|
||||
reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g)
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配 `"42 42 42"`。
|
||||
Your regex should match `"42 42 42"`.
|
||||
|
||||
```js
|
||||
assert(reRegex.test('42 42 42'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配 `"100 100 100"`。
|
||||
Your regex should match `"100 100 100"`.
|
||||
|
||||
```js
|
||||
assert(reRegex.test('100 100 100'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配 `"42 42 42 42"`。
|
||||
Your regex should not match `"42 42 42 42"`.
|
||||
|
||||
```js
|
||||
assert.equal('42 42 42 42'.match(reRegex.source), null);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配 `"42 42"`。
|
||||
Your regex should not match `"42 42"`.
|
||||
|
||||
```js
|
||||
assert.equal('42 42'.match(reRegex.source), null);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配 `"101 102 103"`。
|
||||
Your regex should not match `"101 102 103"`.
|
||||
|
||||
```js
|
||||
assert(!reRegex.test('101 102 103'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配 `"1 2 3"`。
|
||||
Your regex should not match `"1 2 3"`.
|
||||
|
||||
```js
|
||||
assert(!reRegex.test('1 2 3'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配 `"10 10 10"`。
|
||||
Your regex should match `"10 10 10"`.
|
||||
|
||||
```js
|
||||
assert(reRegex.test('10 10 10'));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db9367417b2b2512ba7
|
||||
title: 指定匹配的确切数量
|
||||
title: Specify Exact Number of Matches
|
||||
challengeType: 1
|
||||
forumTopicId: 301365
|
||||
dashedName: specify-exact-number-of-matches
|
||||
@ -8,11 +8,11 @@ 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.
|
||||
|
||||
例如,要只匹配字母`a`出现`3`次的单词`"hah"`,正则表达式应为`/ha{3}h/`。
|
||||
For example, to match only the word `"hah"` with the letter `a` `3` times, your regex would be `/ha{3}h/`.
|
||||
|
||||
```js
|
||||
let A4 = "haaaah";
|
||||
@ -26,43 +26,48 @@ multipleHA.test(A100); // Returns false
|
||||
|
||||
# --instructions--
|
||||
|
||||
修改正则表达式`timRegex`,以匹配仅有四个字母单词`m`的单词`"Timber"`。
|
||||
Change the regex `timRegex` to match the word `"Timber"` only when it has four letter `m`'s.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用花括号。
|
||||
Your regex should use curly brackets.
|
||||
|
||||
```js
|
||||
assert(timRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Timber'`。
|
||||
Your regex should not match `"Timber"`
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(!timRegex.test('Timber'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Timmber'`。
|
||||
Your regex should not match `"Timmber"`
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(!timRegex.test('Timmber'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Timmmber'`。
|
||||
Your regex should not match `"Timmmber"`
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(!timRegex.test('Timmmber'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Timmmmber'`。
|
||||
Your regex should match `"Timmmmber"`
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(timRegex.test('Timmmmber'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配包含 30 个字母`m`的`'Timber'`。
|
||||
Your regex should not match `"Timber"` with 30 `m`'s in it.
|
||||
|
||||
```js
|
||||
timRegex.lastIndex = 0;
|
||||
assert(!timRegex.test('Ti' + 'm'.repeat(30) + 'ber'));
|
||||
```
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db9367417b2b2512ba6
|
||||
title: 只指定匹配的下限
|
||||
title: Specify Only the Lower Number of Matches
|
||||
challengeType: 1
|
||||
forumTopicId: 301366
|
||||
dashedName: specify-only-the-lower-number-of-matches
|
||||
@ -8,11 +8,11 @@ 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.
|
||||
|
||||
例如,要匹配至少出现`3`次字母`a`的字符串`"hah"`,正则表达式应该是`/ha{3,}h/`。
|
||||
For example, to match only the string `"hah"` with the letter `a` appearing at least `3` times, your regex would be `/ha{3,}h/`.
|
||||
|
||||
```js
|
||||
let A4 = "haaaah";
|
||||
@ -26,47 +26,47 @@ multipleA.test(A100); // Returns true
|
||||
|
||||
# --instructions--
|
||||
|
||||
修改正则表达式`haRegex`,匹配包含四个或更多字母`z`的单词`"Hazzah"`。
|
||||
Change the regex `haRegex` to match the word `"Hazzah"` only when it has four or more letter `z`'s.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用花括号。
|
||||
Your regex should use curly brackets.
|
||||
|
||||
```js
|
||||
assert(haRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Hazzah'`。
|
||||
Your regex should not match `"Hazzah"`
|
||||
|
||||
```js
|
||||
assert(!haRegex.test('Hazzah'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Hazzzah'`。
|
||||
Your regex should not match `"Hazzzah"`
|
||||
|
||||
```js
|
||||
assert(!haRegex.test('Hazzzah'));
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `"Hazzzzah"`
|
||||
Your regex should match `"Hazzzzah"`
|
||||
|
||||
```js
|
||||
assert('Hazzzzah'.match(haRegex)[0].length === 8);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Hazzzzah'`。
|
||||
Your regex should match `"Hazzzzzah"`
|
||||
|
||||
```js
|
||||
assert('Hazzzzzah'.match(haRegex)[0].length === 9);
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `"Hazzzzzzah"`
|
||||
Your regex should match `"Hazzzzzzah"`
|
||||
|
||||
```js
|
||||
assert('Hazzzzzzah'.match(haRegex)[0].length === 10);
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `"Hazzah"` with 30 `z`'s in it.
|
||||
Your regex should match `"Hazzah"` with 30 `z`'s in it.
|
||||
|
||||
```js
|
||||
assert('Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah'.match(haRegex)[0].length === 34);
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db9367417b2b2512ba5
|
||||
title: 指定匹配的上限和下限
|
||||
title: Specify Upper and Lower Number of Matches
|
||||
challengeType: 1
|
||||
forumTopicId: 301367
|
||||
dashedName: specify-upper-and-lower-number-of-matches
|
||||
@ -8,11 +8,11 @@ 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.
|
||||
|
||||
例如,要在字符串`"ah"`中匹配仅出现`3`到`5`次的字母`a`,正则表达式应为`/a{3,5}h/`。
|
||||
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/`.
|
||||
|
||||
```js
|
||||
let A4 = "aaaah";
|
||||
@ -24,47 +24,47 @@ multipleA.test(A2); // Returns false
|
||||
|
||||
# --instructions--
|
||||
|
||||
修改正则表达式`ohRegex`以匹配在`"Oh no"`中仅出现`3`到`6`次的字母`h`。
|
||||
Change the regex `ohRegex` to match the entire phrase `"Oh no"` only when it has `3` to `6` letter `h`'s.
|
||||
|
||||
# --hints--
|
||||
|
||||
你的正则表达式应该使用花括号。
|
||||
Your regex should use curly brackets.
|
||||
|
||||
```js
|
||||
assert(ohRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Ohh no'`。
|
||||
Your regex should not match `"Ohh no"`
|
||||
|
||||
```js
|
||||
assert(!ohRegex.test('Ohh no'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Ohhh no'`。
|
||||
Your regex should match `"Ohhh no"`
|
||||
|
||||
```js
|
||||
assert('Ohhh no'.match(ohRegex)[0].length === 7);
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `"Ohhhh no"`。
|
||||
Your regex should match `"Ohhhh no"`
|
||||
|
||||
```js
|
||||
assert('Ohhhh no'.match(ohRegex)[0].length === 8);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Ohhhhh no'`。
|
||||
Your regex should match `"Ohhhhh no"`
|
||||
|
||||
```js
|
||||
assert('Ohhhhh no'.match(ohRegex)[0].length === 9);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Ohhhhhh no'`。
|
||||
Your regex should match `"Ohhhhhh no"`
|
||||
|
||||
```js
|
||||
assert('Ohhhhhh no'.match(ohRegex)[0].length === 10);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Ohhhhhhh no'`。
|
||||
Your regex should not match `"Ohhhhhhh no"`
|
||||
|
||||
```js
|
||||
assert(!ohRegex.test('Ohhhhhhh no'));
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7dbb367417b2b2512bab
|
||||
title: 使用捕获组搜索和替换
|
||||
title: Use Capture Groups to Search and Replace
|
||||
challengeType: 1
|
||||
forumTopicId: 301368
|
||||
dashedName: use-capture-groups-to-search-and-replace
|
||||
@ -8,9 +8,9 @@ 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.
|
||||
|
||||
可以使用字符串上`.replace()`方法来搜索并替换字符串中的文本。`.replace()`的输入首先是想要搜索的正则表达式匹配模式,第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。
|
||||
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.
|
||||
|
||||
```js
|
||||
let wrongText = "The sky is silver.";
|
||||
@ -19,7 +19,7 @@ wrongText.replace(silverRegex, "blue");
|
||||
// Returns "The sky is blue."
|
||||
```
|
||||
|
||||
你还可以使用美元符号(`$`)访问替换字符串中的捕获组。
|
||||
You can also access capture groups in the replacement string with dollar signs (`$`).
|
||||
|
||||
```js
|
||||
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
|
||||
@ -28,28 +28,41 @@ wrongText.replace(silverRegex, "blue");
|
||||
|
||||
# --instructions--
|
||||
|
||||
编写一个正则表达式,以搜索字符串`"good"`。然后更新变量`replaceText`,用字符串`"okey-dokey"`替换`"good"`。
|
||||
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.
|
||||
|
||||
# --hints--
|
||||
|
||||
你应该使用`.replace()`搜索并替换。
|
||||
You should use `.replace()` to search and replace.
|
||||
|
||||
```js
|
||||
assert(code.match(/\.replace\(.*\)/));
|
||||
```
|
||||
|
||||
你的正则表达式应该把`'This sandwich is good.'`变成`'This sandwich is okey-dokey.'`。
|
||||
Your regex should change `"one two three"` to `"three two one"`
|
||||
|
||||
```js
|
||||
assert(
|
||||
result == 'This sandwich is okey-dokey.' && replaceText === 'okey-dokey'
|
||||
);
|
||||
assert(result === 'three two one');
|
||||
```
|
||||
|
||||
你不应该改变最后一行。
|
||||
You should not change the last line.
|
||||
|
||||
```js
|
||||
assert(code.match(/result\s*=\s*huhText\.replace\(.*?\)/));
|
||||
assert(code.match(/result\s*=\s*str\.replace\(.*?\)/));
|
||||
```
|
||||
|
||||
`fixRegex` should use at least three capture groups.
|
||||
|
||||
```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).
|
||||
|
||||
```js
|
||||
{
|
||||
const re = /(\$\d{1,2})+(?:[\D]|\b)/g;
|
||||
assert(replaceText.match(re).length >= 3);
|
||||
}
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 587d7db3367417b2b2512b8e
|
||||
title: 使用测试方法
|
||||
title: Using the Test Method
|
||||
challengeType: 1
|
||||
forumTopicId: 301369
|
||||
dashedName: using-the-test-method
|
||||
@ -8,11 +8,11 @@ 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.
|
||||
|
||||
如果想要在字符串`"The dog chased the cat"`中匹配到`"the"`这个单词,可以使用如下正则表达式:`/the/`。注意,正则表达式中不需要引号。
|
||||
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.
|
||||
|
||||
JavaScript 中有多种使用正则表达式的方法。测试正则表达式的一种方法是使用`.test()`方法。`.test()`方法会把编写的正则表达式和字符串(即括号内的内容)匹配,如果成功匹配到字符,则返回`true`,反之,返回`false`。
|
||||
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.
|
||||
|
||||
```js
|
||||
let testStr = "freeCodeCamp";
|
||||
@ -23,17 +23,17 @@ testRegex.test(testStr);
|
||||
|
||||
# --instructions--
|
||||
|
||||
使用`.test()`方法,检测字符串`myString`是否符合正则表达式`myRegex`定义的规则。
|
||||
Apply the regex `myRegex` on the string `myString` using the `.test()` method.
|
||||
|
||||
# --hints--
|
||||
|
||||
你应该使用`.test()`方法来检测正则表达式。
|
||||
You should use `.test()` to test the regex.
|
||||
|
||||
```js
|
||||
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
```
|
||||
|
||||
你的返回结果应该为`true`。
|
||||
Your result should return `true`.
|
||||
|
||||
```js
|
||||
assert(result === true);
|
||||
|
Reference in New Issue
Block a user