chore(learn): Applied MDX format to Chinese curriculum files (#40462)
This commit is contained in:
@ -1,14 +1,16 @@
|
||||
---
|
||||
id: 587d7dba367417b2b2512ba8
|
||||
title: 检查全部或无
|
||||
challengeType: 1
|
||||
forumTopicId: 301338
|
||||
title: 检查全部或无
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
有时,想要搜寻的匹配模式可能有不确定是否存在的部分。尽管如此,还是想检查它们。
|
||||
为此,可以使用问号<code>?</code>指定可能存在的元素。这将检查前面的零个或一个元素。可以将此符号视为前面的元素是可选的。
|
||||
|
||||
为此,可以使用问号`?`指定可能存在的元素。这将检查前面的零个或一个元素。可以将此符号视为前面的元素是可选的。
|
||||
|
||||
例如,美式英语和英式英语略有不同,可以使用问号来匹配两种拼写。
|
||||
|
||||
```js
|
||||
@ -19,55 +21,35 @@ rainbowRegex.test(american); // Returns true
|
||||
rainbowRegex.test(british); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>favRegex</code>以匹配美式英语(favorite)和英式英语(favourite)的单词版本。
|
||||
</section>
|
||||
修改正则表达式`favRegex`以匹配美式英语(favorite)和英式英语(favourite)的单词版本。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用可选符号<code>?</code>。
|
||||
testString: assert(favRegex.source.match(/\?/).length > 0);
|
||||
- text: "你的正则表达式应该匹配<code>'favorite'</code>。"
|
||||
testString: assert(favRegex.test("favorite"));
|
||||
- text: "你的正则表达式应该匹配<code>'favourite'</code>。"
|
||||
testString: assert(favRegex.test("favourite"));
|
||||
- text: "你的正则表达式不应该匹配<code>'fav'</code>。"
|
||||
testString: assert(!favRegex.test("fav"));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用可选符号`?`。
|
||||
|
||||
```js
|
||||
let favWord = "favorite";
|
||||
let favRegex = /change/; // Change this line
|
||||
let result = favRegex.test(favWord);
|
||||
assert(favRegex.source.match(/\?/).length > 0);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该匹配`'favorite'`。
|
||||
|
||||
```js
|
||||
let favWord = "favorite";
|
||||
let favRegex = /favou?r/;
|
||||
let result = favRegex.test(favWord);
|
||||
assert(favRegex.test('favorite'));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该匹配`'favourite'`。
|
||||
|
||||
```js
|
||||
assert(favRegex.test('favourite'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'fav'`。
|
||||
|
||||
```js
|
||||
assert(!favRegex.test('fav'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 5c3dda8b4d8df89bea71600f
|
||||
title: 检查混合字符组
|
||||
challengeType: 1
|
||||
forumTopicId: 301339
|
||||
title: 检查混合字符组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
有时候我们想使用正则表达式里的括号 <code>()</code> 来检查字符组。
|
||||
如果想在字符串找到 <code>Penguin</code> 或 <code>Pumpkin</code>,可以这个正则表达式:<code>/P(engu|umpk)in/g</code>。
|
||||
然后使用 <code>test()</code> 方法检查 test 字符串里面是否包含字符组。
|
||||
# --description--
|
||||
|
||||
有时候我们想使用正则表达式里的括号 `()` 来检查字符组。
|
||||
|
||||
如果想在字符串找到 `Penguin` 或 `Pumpkin`,可以这个正则表达式:`/P(engu|umpk)in/g`。
|
||||
|
||||
然后使用 `test()` 方法检查 test 字符串里面是否包含字符组。
|
||||
|
||||
```js
|
||||
let testStr = "Pumpkin";
|
||||
@ -18,58 +20,46 @@ testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
完善正则表达式,使其以区分大小写的方式检查 <code>Franklin Roosevelt</code> 或 <code>Eleanor Roosevelt</code> 的名字,并且应该忽略 middle names。
|
||||
然后完善代码,使创建的正则检查 <code>myString</code>,根据正则是否匹配返回 <code>true</code> 或 <code>false</code>。
|
||||
</section>
|
||||
完善正则表达式,使其以区分大小写的方式检查 `Franklin Roosevelt` 或 `Eleanor Roosevelt` 的名字,并且应该忽略 middle names。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
然后完善代码,使创建的正则检查 `myString`,根据正则是否匹配返回 `true` 或 `false`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 正则 <code>myRegex</code> 测试 <code>Franklin D. Roosevelt</code> 应该返回 <code>true</code>。
|
||||
testString: myRegex.lastIndex = 0; assert(myRegex.test('Franklin D. Roosevelt'));
|
||||
- text: 正则 <code>myRegex</code> 测试 <code>Eleanor Roosevelt</code> 应该返回 <code>true</code>。
|
||||
testString: myRegex.lastIndex = 0; assert(myRegex.test('Eleanor Roosevelt'));
|
||||
- text: 正则 <code>myRegex</code> 测试 <code>Franklin Rosevelt</code> 应该返回 <code>false</code>。
|
||||
testString: myRegex.lastIndex = 0; assert(!myRegex.test('Franklin Rosevelt'));
|
||||
- text: 应该使用 <code>.test()</code> 来测试正则。
|
||||
testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
- text: result 应该返回 <code>true</code>。
|
||||
testString: assert(result === true);
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
正则 `myRegex` 测试 `Franklin D. Roosevelt` 应该返回 `true`。
|
||||
|
||||
```js
|
||||
let myString = "Eleanor Roosevelt";
|
||||
let myRegex = /False/; // Change this line
|
||||
let result = false; // Change this line
|
||||
// After passing the challenge experiment with myString and see how the grouping works
|
||||
myRegex.lastIndex = 0;
|
||||
assert(myRegex.test('Franklin D. Roosevelt'));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
正则 `myRegex` 测试 `Eleanor Roosevelt` 应该返回 `true`。
|
||||
|
||||
```js
|
||||
let myString = "Eleanor Roosevelt";
|
||||
let myRegex = /(Franklin|Eleanor).*Roosevelt/;
|
||||
let result = myRegex.test(myString);
|
||||
myRegex.lastIndex = 0;
|
||||
assert(myRegex.test('Eleanor Roosevelt'));
|
||||
```
|
||||
|
||||
</section>
|
||||
正则 `myRegex` 测试 `Franklin Rosevelt` 应该返回 `false`。
|
||||
|
||||
```js
|
||||
myRegex.lastIndex = 0;
|
||||
assert(!myRegex.test('Franklin Rosevelt'));
|
||||
```
|
||||
|
||||
应该使用 `.test()` 来测试正则。
|
||||
|
||||
```js
|
||||
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
```
|
||||
|
||||
result 应该返回 `true`。
|
||||
|
||||
```js
|
||||
assert(result === true);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db4367417b2b2512b92
|
||||
title: 提取匹配项
|
||||
challengeType: 1
|
||||
forumTopicId: 301340
|
||||
title: 提取匹配项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
到目前为止,只是检查了一个匹配模式是否存在于字符串中。还可以使用<code>.match()</code>方法来提取找到的实际匹配项。
|
||||
可以使用字符串来调用<code>.match()</code>方法,并在括号内传入正则表达式。以下是一个示例:
|
||||
# --description--
|
||||
|
||||
到目前为止,只是检查了一个匹配模式是否存在于字符串中。还可以使用`.match()`方法来提取找到的实际匹配项。
|
||||
|
||||
可以使用字符串来调用`.match()`方法,并在括号内传入正则表达式。以下是一个示例:
|
||||
|
||||
```js
|
||||
"Hello, World!".match(/Hello/);
|
||||
@ -19,53 +20,29 @@ ourStr.match(ourRegex);
|
||||
// Returns ["expressions"]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
利用<code>.match()</code>方法提取单词<code>coding</code>。
|
||||
</section>
|
||||
利用`.match()`方法提取单词`coding`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>结果</code>应该包含单词<code>coding</code>。
|
||||
testString: assert(result.join() === "coding");
|
||||
- text: 你的正则表达式<code>codingRegex</code>应该搜寻<code>coding</code>。
|
||||
testString: assert(codingRegex.source === "coding");
|
||||
- text: 你应该使用<code>.match()</code>方法。
|
||||
testString: assert(code.match(/\.match\(.*\)/));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`结果`应该包含单词`coding`。
|
||||
|
||||
```js
|
||||
let extractStr = "Extract the word 'coding' from this string.";
|
||||
let codingRegex = /change/; // Change this line
|
||||
let result = extractStr; // Change this line
|
||||
assert(result.join() === 'coding');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式`codingRegex`应该搜寻`coding`。
|
||||
|
||||
```js
|
||||
let extractStr = "Extract the word 'coding' from this string.";
|
||||
let codingRegex = /coding/; // Change this line
|
||||
let result = extractStr.match(codingRegex); // Change this line
|
||||
assert(codingRegex.source === 'coding');
|
||||
```
|
||||
|
||||
</section>
|
||||
你应该使用`.match()`方法。
|
||||
|
||||
```js
|
||||
assert(code.match(/\.match\(.*\)/));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,60 +1,34 @@
|
||||
---
|
||||
id: 587d7db6367417b2b2512b9b
|
||||
title: 用惰性匹配来查找字符
|
||||
challengeType: 1
|
||||
forumTopicId: 301341
|
||||
title: 用惰性匹配来查找字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在正则表达式中,<code>贪婪</code>匹配会匹配到符合正则表达式匹配模式的字符串的最长可能部分,并将其作为匹配项返回。另一种方案称为<code>懒惰</code>匹配,它会匹配到满足正则表达式的字符串的最小可能部分。
|
||||
可以将正则表达式<code>/t[a-z]*i/</code>应用于字符串<code>"titanic"</code>。这个正则表达式是一个以<code>t</code>开始,以<code>i</code>结束,并且中间有一些字母的匹配模式。
|
||||
正则表达式默认是<code>贪婪</code>匹配,因此匹配返回为<code>["titani"]</code>。它会匹配到适合该匹配模式的最大子字符串。
|
||||
但是,你可以使用<code>?</code>字符来将其变成<code>懒惰</code>匹配。调整后的正则表达式<code>/t[a-z]*?i/</code>匹配字符串<code>"titanic"</code>返回<code>["ti"]</code>。
|
||||
<strong>注意</strong><br>应该避免使用正则表达式解析 HTML,但是可以用正则表达式匹配 HTML 字符串。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修复正则表达式<code>/<.*>/</code>,让它返回 HTML 标签<code><h1></code>,而不是文本<code>"<h1>Winter is coming</h1>"</code>。请记得在正则表达式中使用通配符<code>.</code>来匹配任意字符。
|
||||
</section>
|
||||
在正则表达式中,`贪婪`匹配会匹配到符合正则表达式匹配模式的字符串的最长可能部分,并将其作为匹配项返回。另一种方案称为`懒惰`匹配,它会匹配到满足正则表达式的字符串的最小可能部分。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
可以将正则表达式`/t[a-z]*i/`应用于字符串`"titanic"`。这个正则表达式是一个以`t`开始,以`i`结束,并且中间有一些字母的匹配模式。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>结果</code>变量应该是一个包含<code><h1></code>的数组。
|
||||
testString: assert(result[0] == '<h1>');
|
||||
正则表达式默认是`贪婪`匹配,因此匹配返回为`["titani"]`。它会匹配到适合该匹配模式的最大子字符串。
|
||||
|
||||
```
|
||||
但是,你可以使用`?`字符来将其变成`懒惰`匹配。调整后的正则表达式`/t[a-z]*?i/`匹配字符串`"titanic"`返回`["ti"]`。
|
||||
|
||||
</section>
|
||||
**注意**
|
||||
应该避免使用正则表达式解析 HTML,但是可以用正则表达式匹配 HTML 字符串。
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --instructions--
|
||||
|
||||
<div id='js-seed'>
|
||||
修复正则表达式`/<.*>/`,让它返回 HTML 标签`<h1>`,而不是文本`"<h1>Winter is coming</h1>"`。请记得在正则表达式中使用通配符`.`来匹配任意字符。
|
||||
|
||||
# --hints--
|
||||
|
||||
`结果`变量应该是一个包含`<h1>`的数组。
|
||||
|
||||
```js
|
||||
let text = "<h1>Winter is coming</h1>";
|
||||
let myRegex = /<.*>/; // Change this line
|
||||
let result = text.match(myRegex);
|
||||
assert(result[0] == '<h1>');
|
||||
```
|
||||
|
||||
</div>
|
||||
# --solutions--
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
let text = "<h1>Winter is coming</h1>";
|
||||
let myRegex = /<.*?>/; // Change this line
|
||||
let result = text.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -1,12 +1,12 @@
|
||||
---
|
||||
id: 587d7db4367417b2b2512b93
|
||||
title: 全局匹配
|
||||
challengeType: 1
|
||||
forumTopicId: 301342
|
||||
title: 全局匹配
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
到目前为止,只能提取或搜寻一次模式匹配。
|
||||
|
||||
```js
|
||||
@ -16,7 +16,7 @@ testStr.match(ourRegex);
|
||||
// Returns ["Repeat"]
|
||||
```
|
||||
|
||||
若要多次搜寻或提取模式匹配,可以使用<code>g</code>标志。
|
||||
若要多次搜寻或提取模式匹配,可以使用`g`标志。
|
||||
|
||||
```js
|
||||
let repeatRegex = /Repeat/g;
|
||||
@ -24,56 +24,44 @@ testStr.match(repeatRegex);
|
||||
// Returns ["Repeat", "Repeat", "Repeat"]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用正则表达式<code>starRegex</code>,从字符串<code>twinkleStar</code>中匹配到所有的<code>"Twinkle"</code>单词并提取出来。
|
||||
<strong>注意:</strong><br>在正则表达式上可以有多个标志,比如<code>/search/gi</code>。
|
||||
</section>
|
||||
使用正则表达式`starRegex`,从字符串`twinkleStar`中匹配到所有的`"Twinkle"`单词并提取出来。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**注意:**
|
||||
在正则表达式上可以有多个标志,比如`/search/gi`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>starRegex</code>应该使用全局标志<code>g</code>。
|
||||
testString: assert(starRegex.flags.match(/g/).length == 1);
|
||||
- text: 你的正则表达式<code>starRegex</code>应该使用忽略大小写标志<code>i</code>。
|
||||
testString: assert(starRegex.flags.match(/i/).length == 1);
|
||||
- text: "你的匹配应该匹配单词<code>'Twinkle'</code>的两个匹配项。"
|
||||
testString: assert(result.sort().join() == twinkleStar.match(/twinkle/gi).sort().join());
|
||||
- text: 你的匹配<code>结果</code>应该包含两个元素。
|
||||
testString: assert(result.length == 2);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式`starRegex`应该使用全局标志`g`。
|
||||
|
||||
```js
|
||||
let twinkleStar = "Twinkle, twinkle, little star";
|
||||
let starRegex = /change/; // Change this line
|
||||
let result = twinkleStar; // Change this line
|
||||
assert(starRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式`starRegex`应该使用忽略大小写标志`i`。
|
||||
|
||||
```js
|
||||
let twinkleStar = "Twinkle, twinkle, little star";
|
||||
let starRegex = /twinkle/gi;
|
||||
let result = twinkleStar.match(starRegex);
|
||||
assert(starRegex.flags.match(/i/).length == 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
你的匹配应该匹配单词`'Twinkle'`的两个匹配项。
|
||||
|
||||
```js
|
||||
assert(
|
||||
result.sort().join() ==
|
||||
twinkleStar
|
||||
.match(/twinkle/gi)
|
||||
.sort()
|
||||
.join()
|
||||
);
|
||||
```
|
||||
|
||||
你的匹配`结果`应该包含两个元素。
|
||||
|
||||
```js
|
||||
assert(result.length == 2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 587d7db7367417b2b2512b9c
|
||||
title: 在狩猎中找到一个或多个罪犯
|
||||
challengeType: 1
|
||||
forumTopicId: 301343
|
||||
title: 在狩猎中找到一个或多个罪犯
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
是时候暂停和测试你的新正则表达式写作技巧了。一群罪犯逃出监狱逃跑,但你不知道有多少人。但是,你知道他们和其他人在一起时会保持紧密联系。你有责任立刻找到所有的罪犯。
|
||||
|
||||
这里有一个示例来回顾如何做到这一点:
|
||||
当字母<code>z</code>在一行中出现一次或连续多次时,正则表达式<code>/z+/</code>会匹配到它。它会在以下所有字符串中找到匹配项:
|
||||
|
||||
当字母`z`在一行中出现一次或连续多次时,正则表达式`/z+/`会匹配到它。它会在以下所有字符串中找到匹配项:
|
||||
|
||||
```js
|
||||
"z"
|
||||
@ -19,7 +21,7 @@ title: 在狩猎中找到一个或多个罪犯
|
||||
"abczzzzzzzzzzzzzzzzzzzzzabc"
|
||||
```
|
||||
|
||||
但是它不会在以下字符串中找到匹配项,因为它们中没有字母<code>z</code>:
|
||||
但是它不会在以下字符串中找到匹配项,因为它们中没有字母`z`:
|
||||
|
||||
```js
|
||||
""
|
||||
@ -27,69 +29,66 @@ title: 在狩猎中找到一个或多个罪犯
|
||||
"abcabc"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
编写一个<code>贪婪</code>正则表达式,在一组其他人中匹配到一个或多个罪犯。罪犯由大写字母<code>C</code>表示。
|
||||
</section>
|
||||
编写一个`贪婪`正则表达式,在一组其他人中匹配到一个或多个罪犯。罪犯由大写字母`C`表示。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: "你的正则表达式应该匹配<code>'C'</code>中的 <em>一个</em> 罪犯('<code>C</code>')。"
|
||||
testString: assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
|
||||
- text: "你的正则表达式应该匹配<code>'CC'</code>中的 <em>两个</em> 罪犯('<code>CC</code>')。"
|
||||
testString: assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
|
||||
- text: "你的正则表达式应该匹配<code>'P1P5P4CCCP2P6P3'</code>中的 <em>三个</em> 罪犯('<code>CCC</code>')。"
|
||||
testString: assert('P1P5P4CCCP2P6P3'.match(reCriminals) && 'P1P5P4CCCP2P6P3'.match(reCriminals)[0] == 'CCC');
|
||||
- text: "你的正则表达式应该匹配<code>'P6P2P7P4P5CCCCCP3P1'</code>中的 <em>五个</em> 罪犯('<code>CCCCC</code>')。"
|
||||
testString: assert('P6P2P7P4P5CCCCCP3P1'.match(reCriminals) && 'P6P2P7P4P5CCCCCP3P1'.match(reCriminals)[0] == 'CCCCC');
|
||||
- text: "你的正则表达式在<code>''</code>中不应该匹配到任何罪犯。"
|
||||
testString: assert(!reCriminals.test(''));
|
||||
- text: "你的正则表达式在<code>'P1P2P3'</code>中不应该匹配到任何罪犯。"
|
||||
testString: assert(!reCriminals.test('P1P2P3'));
|
||||
- text: "你的正则表达式应该匹配<code>'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'</code>中的 <em>五十个</em> 罪犯('<code>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC</code>')。"
|
||||
testString: assert('P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(reCriminals) && 'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(reCriminals)[0] == "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC");
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该匹配`'C'`中的 <em>一个</em> 罪犯('`C`')。
|
||||
|
||||
```js
|
||||
// example crowd gathering
|
||||
let crowd = 'P1P2P3P4P5P6CCCP7P8P9';
|
||||
|
||||
let reCriminals = /./; // Change this line
|
||||
|
||||
let matchedCriminals = crowd.match(reCriminals);
|
||||
console.log(matchedCriminals);
|
||||
assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该匹配`'CC'`中的 <em>两个</em> 罪犯('`CC`')。
|
||||
|
||||
```js
|
||||
// example crowd gathering
|
||||
let crowd = 'P1P2P3P4P5P6CCCP7P8P9';
|
||||
|
||||
let reCriminals = /C+/; // Change this line
|
||||
|
||||
let matchedCriminals = crowd.match(reCriminals);
|
||||
|
||||
assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该匹配`'P1P5P4CCCP2P6P3'`中的 <em>三个</em> 罪犯('`CCC`')。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'P1P5P4CCCP2P6P3'.match(reCriminals) &&
|
||||
'P1P5P4CCCP2P6P3'.match(reCriminals)[0] == 'CCC'
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'P6P2P7P4P5CCCCCP3P1'`中的 <em>五个</em> 罪犯('`CCCCC`')。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'P6P2P7P4P5CCCCCP3P1'.match(reCriminals) &&
|
||||
'P6P2P7P4P5CCCCCP3P1'.match(reCriminals)[0] == 'CCCCC'
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式在`''`中不应该匹配到任何罪犯。
|
||||
|
||||
```js
|
||||
assert(!reCriminals.test(''));
|
||||
```
|
||||
|
||||
你的正则表达式在`'P1P2P3'`中不应该匹配到任何罪犯。
|
||||
|
||||
```js
|
||||
assert(!reCriminals.test('P1P2P3'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'`中的 <em>五十个</em> 罪犯('`CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC`')。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(
|
||||
reCriminals
|
||||
) &&
|
||||
'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(
|
||||
reCriminals
|
||||
)[0] == 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,76 +1,83 @@
|
||||
---
|
||||
id: 587d7db4367417b2b2512b91
|
||||
title: 匹配时忽略大小写
|
||||
challengeType: 1
|
||||
forumTopicId: 301344
|
||||
title: 匹配时忽略大小写
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
到目前为止,已经了解了如何用正则表达式来执行字符串的匹配。但有时候,并不关注匹配字母的大小写。
|
||||
大小写即大写字母和小写字母。大写字母如<code>"A"</code>、<code>"B"</code>和<code>"C"</code>。小写字母如<code>"a"</code>、<code>"b"</code>和<code>"c"</code>。
|
||||
可以使用标志(flag)来匹配这两种情况。标志有很多,不过这里我们只关注忽略大小写的标志——<code>i</code>。可以通过将它附加到正则表达式之后来使用它。这里给出使用该标志的一个实例<code>/ignorecase/i</code>。这个字符串可以匹配字符串<code>"ignorecase"</code>、<code>"igNoreCase"</code>和<code>"IgnoreCase"</code>。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
编写正则表达式<code>fccRegex</code>以匹配<code>"freeCodeCamp"</code>,忽略大小写。正则表达式不应与任何缩写或带有空格的变体匹配。
|
||||
</section>
|
||||
大小写即大写字母和小写字母。大写字母如`"A"`、`"B"`和`"C"`。小写字母如`"a"`、`"b"`和`"c"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
可以使用标志(flag)来匹配这两种情况。标志有很多,不过这里我们只关注忽略大小写的标志——`i`。可以通过将它附加到正则表达式之后来使用它。这里给出使用该标志的一个实例`/ignorecase/i`。这个字符串可以匹配字符串`"ignorecase"`、`"igNoreCase"`和`"IgnoreCase"`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该匹配<code>freeCodeCamp</code>。
|
||||
testString: assert(fccRegex.test('freeCodeCamp'));
|
||||
- text: 你的正则表达式应该匹配<code>FreeCodeCamp</code>。
|
||||
testString: assert(fccRegex.test('FreeCodeCamp'));
|
||||
- text: 你的正则表达式应该匹配<code>FreecodeCamp</code>。
|
||||
testString: assert(fccRegex.test('FreecodeCamp'));
|
||||
- text: 你的正则表达式应该匹配<code>FreeCodecamp</code>。
|
||||
testString: assert(fccRegex.test('FreeCodecamp'));
|
||||
- text: 你的正则表达式不应该匹配<code>Free Code Camp</code>。
|
||||
testString: assert(!fccRegex.test('Free Code Camp'));
|
||||
- text: Your regex should match<code>FreeCOdeCamp</code>。
|
||||
testString: assert(fccRegex.test('FreeCOdeCamp'));
|
||||
- text: 你的正则表达式不应该匹配<code>FCC</code>。
|
||||
testString: assert(!fccRegex.test('FCC'));
|
||||
- text: 你的正则表达式应该匹配<code>FrEeCoDeCamp</code>。
|
||||
testString: assert(fccRegex.test('FrEeCoDeCamp'));
|
||||
- text: 你的正则表达式应该匹配<code>FrEeCodECamp</code>。
|
||||
testString: assert(fccRegex.test('FrEeCodECamp'));
|
||||
- text: 你的正则表达式应该匹配<code>FReeCodeCAmp</code>。
|
||||
testString: assert(fccRegex.test('FReeCodeCAmp'));
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
编写正则表达式`fccRegex`以匹配`"freeCodeCamp"`,忽略大小写。正则表达式不应与任何缩写或带有空格的变体匹配。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该匹配`freeCodeCamp`。
|
||||
|
||||
```js
|
||||
let myString = "freeCodeCamp";
|
||||
let fccRegex = /change/; // Change this line
|
||||
let result = fccRegex.test(myString);
|
||||
assert(fccRegex.test('freeCodeCamp'));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该匹配`FreeCodeCamp`。
|
||||
|
||||
```js
|
||||
let myString = "freeCodeCamp";
|
||||
let fccRegex = /freecodecamp/i; // Change this line
|
||||
let result = fccRegex.test(myString);
|
||||
assert(fccRegex.test('FreeCodeCamp'));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该匹配`FreecodeCamp`。
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreecodeCamp'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FreeCodecamp`。
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreeCodecamp'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`Free Code Camp`。
|
||||
|
||||
```js
|
||||
assert(!fccRegex.test('Free Code Camp'));
|
||||
```
|
||||
|
||||
Your regex should match`FreeCOdeCamp`。
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FreeCOdeCamp'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`FCC`。
|
||||
|
||||
```js
|
||||
assert(!fccRegex.test('FCC'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FrEeCoDeCamp`。
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FrEeCoDeCamp'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FrEeCodECamp`。
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FrEeCodECamp'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`FReeCodeCAmp`。
|
||||
|
||||
```js
|
||||
assert(fccRegex.test('FReeCodeCAmp'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,71 +1,67 @@
|
||||
---
|
||||
id: 587d7db4367417b2b2512b90
|
||||
title: 同时用多种模式匹配文字字符串
|
||||
challengeType: 1
|
||||
forumTopicId: 301345
|
||||
title: 同时用多种模式匹配文字字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用正则表达式<code>/coding/</code>,你可以在其他字符串中查找<code>"coding"</code>。
|
||||
这对于搜寻单个字符串非常有用,但仅限于一种匹配模式。你可以使用<code>|</code>操作符来匹配多个规则。
|
||||
此操作符匹配操作符前面或后面的字符。例如,如果你想匹配<code>"yes"</code>或<code>"no"</code>,你需要的正则表达式是<code>/yes|no/</code>。
|
||||
你还可以匹配多个规则,这可以通过添加更多的匹配模式来实现。这些匹配模式将包含更多的<code>|</code>操作符来分隔它们,比如<code>/yes|no|maybe/</code>。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
完成正则表达式<code>petRegex</code>以匹配<code>"dog"</code>、<code>"cat"</code>、<code>"bird"</code>或者<code>"fish"</code>。
|
||||
</section>
|
||||
使用正则表达式`/coding/`,你可以在其他字符串中查找`"coding"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
这对于搜寻单个字符串非常有用,但仅限于一种匹配模式。你可以使用`|`操作符来匹配多个规则。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: "对于字符串<code>'John has a pet dog.'</code>,你的正则表达式<code>petRegex</code>的<code>test</code>方法应该返回<code>true</code>。"
|
||||
testString: assert(petRegex.test('John has a pet dog.'));
|
||||
- text: "对于字符串<code>'Emma has a pet rock.'</code>,你的正则表达式<code>petRegex</code>的<code>test</code>方法应该返回<code>false</code>。"
|
||||
testString: assert(!petRegex.test('Emma has a pet rock.'));
|
||||
- text: "对于字符串<code>'Emma has a pet bird.'</code>,你的正则表达式<code>petRegex</code>的<code>test</code>方法应该返回<code>true</code>。"
|
||||
testString: assert(petRegex.test('Emma has a pet bird.'));
|
||||
- text: "对于字符串<code>'Liz has a pet cat.'</code>,你的正则表达式<code>petRegex</code>的<code>test</code>方法应该返回<code>true</code>。"
|
||||
testString: assert(petRegex.test('Liz has a pet cat.'));
|
||||
- text: "对于字符串<code>'Kara has a pet dolphin.'</code>,你的正则表达式<code>petRegex</code>的<code>test</code>方法应该返回<code>false</code>。"
|
||||
testString: assert(!petRegex.test('Kara has a pet dolphin.'));
|
||||
- text: "对于字符串<code>'Alice has a pet fish.'</code>,你的正则表达式<code>petRegex</code>的<code>test</code>方法应该返回<code>true</code>。"
|
||||
testString: assert(petRegex.test('Alice has a pet fish.'));
|
||||
- text: "对于字符串<code>'Jimmy has a pet computer.'</code>,你的正则表达式<code>petRegex</code>的<code>test</code>方法应该返回<code>false</code>。"
|
||||
testString: assert(!petRegex.test('Jimmy has a pet computer.'));
|
||||
此操作符匹配操作符前面或后面的字符。例如,如果你想匹配`"yes"`或`"no"`,你需要的正则表达式是`/yes|no/`。
|
||||
|
||||
```
|
||||
你还可以匹配多个规则,这可以通过添加更多的匹配模式来实现。这些匹配模式将包含更多的`|`操作符来分隔它们,比如`/yes|no|maybe/`。
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
完成正则表达式`petRegex`以匹配`"dog"`、`"cat"`、`"bird"`或者`"fish"`。
|
||||
|
||||
<div id='js-seed'>
|
||||
# --hints--
|
||||
|
||||
对于字符串`'John has a pet dog.'`,你的正则表达式`petRegex`的`test`方法应该返回`true`。
|
||||
|
||||
```js
|
||||
let petString = "James has a pet cat.";
|
||||
let petRegex = /change/; // Change this line
|
||||
let result = petRegex.test(petString);
|
||||
assert(petRegex.test('John has a pet dog.'));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
对于字符串`'Emma has a pet rock.'`,你的正则表达式`petRegex`的`test`方法应该返回`false`。
|
||||
|
||||
```js
|
||||
let petString = "James has a pet cat.";
|
||||
let petRegex = /dog|cat|bird|fish/; // Change this line
|
||||
let result = petRegex.test(petString);
|
||||
assert(!petRegex.test('Emma has a pet rock.'));
|
||||
```
|
||||
|
||||
</section>
|
||||
对于字符串`'Emma has a pet bird.'`,你的正则表达式`petRegex`的`test`方法应该返回`true`。
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Emma has a pet bird.'));
|
||||
```
|
||||
|
||||
对于字符串`'Liz has a pet cat.'`,你的正则表达式`petRegex`的`test`方法应该返回`true`。
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Liz has a pet cat.'));
|
||||
```
|
||||
|
||||
对于字符串`'Kara has a pet dolphin.'`,你的正则表达式`petRegex`的`test`方法应该返回`false`。
|
||||
|
||||
```js
|
||||
assert(!petRegex.test('Kara has a pet dolphin.'));
|
||||
```
|
||||
|
||||
对于字符串`'Alice has a pet fish.'`,你的正则表达式`petRegex`的`test`方法应该返回`true`。
|
||||
|
||||
```js
|
||||
assert(petRegex.test('Alice has a pet fish.'));
|
||||
```
|
||||
|
||||
对于字符串`'Jimmy has a pet computer.'`,你的正则表达式`petRegex`的`test`方法应该返回`false`。
|
||||
|
||||
```js
|
||||
assert(!petRegex.test('Jimmy has a pet computer.'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db7367417b2b2512b9f
|
||||
title: 匹配所有的字母和数字
|
||||
challengeType: 1
|
||||
forumTopicId: 301346
|
||||
title: 匹配所有的字母和数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用元字符,可以使用<code>[a-z]</code>搜寻字母表中的所有字母。这种元字符是很常见的,它有一个缩写,但这个缩写也包含额外的字符。
|
||||
JavaScript 中与字母表匹配的最接近的元字符是<code>\w</code>,这个缩写等同于<code>[A-Za-z0-9_]</code>。它不仅可以匹配大小写字母和数字,注意,它还会匹配下划线字符(<code>_</code>)。
|
||||
# --description--
|
||||
|
||||
使用元字符,可以使用`[a-z]`搜寻字母表中的所有字母。这种元字符是很常见的,它有一个缩写,但这个缩写也包含额外的字符。
|
||||
|
||||
JavaScript 中与字母表匹配的最接近的元字符是`\w`,这个缩写等同于`[A-Za-z0-9_]`。它不仅可以匹配大小写字母和数字,注意,它还会匹配下划线字符(`_`)。
|
||||
|
||||
```js
|
||||
let longHand = /[A-Za-z0-9_]+/;
|
||||
@ -21,59 +22,57 @@ longHand.test(varNames); // Returns true
|
||||
shortHand.test(varNames); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用缩写<code>\w</code>来计算所有引号中字母和数字字符的数量。
|
||||
</section>
|
||||
使用缩写`\w`来计算所有引号中字母和数字字符的数量。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(alphabetRegexV2.global);
|
||||
- text: 正则表达式应该使用元字符 <code>\w</code> 匹配字母表里的所有字符。
|
||||
testString: assert(/\\w/.test(alphabetRegexV2.source));
|
||||
- text: "你的正则表达式应该在<code>'The five boxing wizards jump quickly.'</code>中匹配到 31 个字母数字字符。"
|
||||
testString: assert("The five boxing wizards jump quickly.".match(alphabetRegexV2).length === 31);
|
||||
- text: "你的正则表达式应该在<code>'Pack my box with five dozen liquor jugs.'</code>中匹配到 32 个字母数字字符。"
|
||||
testString: assert("Pack my box with five dozen liquor jugs.".match(alphabetRegexV2).length === 32);
|
||||
- text: "你的正则表达式应该在<code>'How vexingly quick daft zebras jump!'</code>中匹配到 30 个字母数字字符。"
|
||||
testString: assert("How vexingly quick daft zebras jump!".match(alphabetRegexV2).length === 30);
|
||||
- text: "你的正则表达式应该在<code>'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'</code>中匹配到 36 个字母数字字符。"
|
||||
testString: assert("123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.".match(alphabetRegexV2).length === 36);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
|
||||
```js
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let alphabetRegexV2 = /change/; // Change this line
|
||||
let result = quoteSample.match(alphabetRegexV2).length;
|
||||
assert(alphabetRegexV2.global);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
正则表达式应该使用元字符 `\w` 匹配字母表里的所有字符。
|
||||
|
||||
```js
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let alphabetRegexV2 = /\w/g; // Change this line
|
||||
let result = quoteSample.match(alphabetRegexV2).length;
|
||||
assert(/\\w/.test(alphabetRegexV2.source));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该在`'The five boxing wizards jump quickly.'`中匹配到 31 个字母数字字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'The five boxing wizards jump quickly.'.match(alphabetRegexV2).length === 31
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Pack my box with five dozen liquor jugs.'`中匹配到 32 个字母数字字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'Pack my box with five dozen liquor jugs.'.match(alphabetRegexV2).length ===
|
||||
32
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'How vexingly quick daft zebras jump!'`中匹配到 30 个字母数字字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'How vexingly quick daft zebras jump!'.match(alphabetRegexV2).length === 30
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'`中匹配到 36 个字母数字字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'.match(alphabetRegexV2)
|
||||
.length === 36
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,71 +1,69 @@
|
||||
---
|
||||
id: 587d7db8367417b2b2512ba1
|
||||
title: 匹配所有非数字
|
||||
challengeType: 1
|
||||
forumTopicId: 301347
|
||||
title: 匹配所有非数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
上一项挑战中展示了如何使用带有小写<code>d</code>的缩写<code>\d</code>来搜寻数字。也可以使用类似的缩写来搜寻非数字,该缩写使用大写的<code>D</code>。
|
||||
查找非数字字符的缩写是<code>\D</code>。这等同于字符串<code>[^0-9]</code>,它查找不是 0 - 9 之间数字的单个字符。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用非数字缩写<code>\D</code>来计算电影标题中有多少非数字。
|
||||
</section>
|
||||
上一项挑战中展示了如何使用带有小写`d`的缩写`\d`来搜寻数字。也可以使用类似的缩写来搜寻非数字,该缩写使用大写的`D`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
查找非数字字符的缩写是`\D`。这等同于字符串`[^0-9]`,它查找不是 0 - 9 之间数字的单个字符。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用缩写来匹配非数字字符。
|
||||
testString: assert(/\\D/.test(noNumRegex.source));
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(noNumRegex.global);
|
||||
- text: "你的正则表达式在<code>'9'</code>中应该匹配不到非数字。"
|
||||
testString: assert("9".match(noNumRegex) == null);
|
||||
- text: "你的正则表达式应该在<code>'Catch 22'</code>中匹配到 6 个非数字。"
|
||||
testString: assert("Catch 22".match(noNumRegex).length == 6);
|
||||
- text: "你的正则表达式应该在<code>'101 Dalmatians'</code>中匹配到 11 个非数字。"
|
||||
testString: assert("101 Dalmatians".match(noNumRegex).length == 11);
|
||||
- text: "你的正则表达式应该在<code>'One, Two, Three'</code>中匹配到 15 个非数字。"
|
||||
testString: assert("One, Two, Three".match(noNumRegex).length == 15);
|
||||
- text: "你的正则表达式应该在<code>'21 Jump Street'</code>中匹配到 12 个非数字。"
|
||||
testString: assert("21 Jump Street".match(noNumRegex).length == 12);
|
||||
- text: '你的正则表达式应该在<code>"2001: A Space Odyssey"</code>中匹配到 17 个非数字。'
|
||||
testString: 'assert("2001: A Space Odyssey".match(noNumRegex).length == 17);'
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
使用非数字缩写`\D`来计算电影标题中有多少非数字。
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用缩写来匹配非数字字符。
|
||||
|
||||
```js
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let noNumRegex = /change/; // Change this line
|
||||
let result = movieName.match(noNumRegex).length;
|
||||
assert(/\\D/.test(noNumRegex.source));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
|
||||
```js
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let noNumRegex = /\D/g; // Change this line
|
||||
let result = movieName.match(noNumRegex).length;
|
||||
assert(noNumRegex.global);
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式在`'9'`中应该匹配不到非数字。
|
||||
|
||||
```js
|
||||
assert('9'.match(noNumRegex) == null);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Catch 22'`中匹配到 6 个非数字。
|
||||
|
||||
```js
|
||||
assert('Catch 22'.match(noNumRegex).length == 6);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'101 Dalmatians'`中匹配到 11 个非数字。
|
||||
|
||||
```js
|
||||
assert('101 Dalmatians'.match(noNumRegex).length == 11);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'One, Two, Three'`中匹配到 15 个非数字。
|
||||
|
||||
```js
|
||||
assert('One, Two, Three'.match(noNumRegex).length == 15);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'21 Jump Street'`中匹配到 12 个非数字。
|
||||
|
||||
```js
|
||||
assert('21 Jump Street'.match(noNumRegex).length == 12);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`"2001: A Space Odyssey"`中匹配到 17 个非数字。
|
||||
|
||||
```js
|
||||
assert('2001: A Space Odyssey'.match(noNumRegex).length == 17);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,72 +1,69 @@
|
||||
---
|
||||
id: 5d712346c441eddfaeb5bdef
|
||||
title: 匹配所有数字
|
||||
challengeType: 1
|
||||
forumTopicId: 18181
|
||||
title: 匹配所有数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
已经了解了常见字符串匹配模式的元字符,如字母数字。另一个常见的匹配模式是只寻找数字。
|
||||
查找数字字符的缩写是<code>\d</code>,注意是小写的<code>d</code>。这等同于元字符<code>[0-9]</code>,它查找 0 到 9 之间任意数字的单个字符。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用缩写<code>\d</code>来计算电影标题中有多少个数字。书面数字("six" 而不是 6)不计算在内。
|
||||
</section>
|
||||
查找数字字符的缩写是`\d`,注意是小写的`d`。这等同于元字符`[0-9]`,它查找 0 到 9 之间任意数字的单个字符。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用缩写来匹配数字字符。
|
||||
testString: assert(/\\d/.test(numRegex.source));
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(numRegex.global);
|
||||
- text: "你的正则表达式应该在<code>'9'</code>中匹配到 1 个数字。"
|
||||
testString: assert("9".match(numRegex).length == 1);
|
||||
- text: "你的正则表达式应该在<code>'Catch 22'</code>中匹配到 2 个数字。"
|
||||
testString: assert("Catch 22".match(numRegex).length == 2);
|
||||
- text: "你的正则表达式应该在<code>'101 Dalmatians'</code>中匹配到 3 个数字。"
|
||||
testString: assert("101 Dalmatians".match(numRegex).length == 3);
|
||||
- text: "你的正则表达式在<code>'One, Two, Three'</code>中应该匹配不到数字。"
|
||||
testString: assert("One, Two, Three".match(numRegex) == null);
|
||||
- text: "你的正则表达式应该在<code>'21 Jump Street'</code>中匹配到 2 个数字。"
|
||||
testString: assert("21 Jump Street".match(numRegex).length == 2);
|
||||
- text: '你的正则表达式应该在<code>"2001: A Space Odyssey"</code>中匹配到 4 个数字。'
|
||||
testString: 'assert("2001: A Space Odyssey".match(numRegex).length == 4);'
|
||||
使用缩写`\d`来计算电影标题中有多少个数字。书面数字("six" 而不是 6)不计算在内。
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用缩写来匹配数字字符。
|
||||
|
||||
```js
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let numRegex = /change/; // Change this line
|
||||
let result = movieName.match(numRegex).length;
|
||||
assert(/\\d/.test(numRegex.source));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
|
||||
```js
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let numRegex = /\d/g; // Change this line
|
||||
let result = movieName.match(numRegex).length;
|
||||
|
||||
assert(numRegex.global);
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该在`'9'`中匹配到 1 个数字。
|
||||
|
||||
```js
|
||||
assert('9'.match(numRegex).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Catch 22'`中匹配到 2 个数字。
|
||||
|
||||
```js
|
||||
assert('Catch 22'.match(numRegex).length == 2);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'101 Dalmatians'`中匹配到 3 个数字。
|
||||
|
||||
```js
|
||||
assert('101 Dalmatians'.match(numRegex).length == 3);
|
||||
```
|
||||
|
||||
你的正则表达式在`'One, Two, Three'`中应该匹配不到数字。
|
||||
|
||||
```js
|
||||
assert('One, Two, Three'.match(numRegex) == null);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'21 Jump Street'`中匹配到 2 个数字。
|
||||
|
||||
```js
|
||||
assert('21 Jump Street'.match(numRegex).length == 2);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`"2001: A Space Odyssey"`中匹配到 4 个数字。
|
||||
|
||||
```js
|
||||
assert('2001: A Space Odyssey'.match(numRegex).length == 4);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db5367417b2b2512b94
|
||||
title: 用通配符.匹配任何内容
|
||||
challengeType: 1
|
||||
forumTopicId: 301348
|
||||
title: 用通配符.匹配任何内容
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
有时不(或不需要)知道匹配模式中的确切字符。如果要精确匹配到完整的单词,那出现一个拼写错误就会匹配不到。幸运的是,可以使用通配符<code>.</code>来处理这种情况。
|
||||
通配符<code>.</code>将匹配任何一个字符。通配符也叫<code>dot</code>或<code>period</code>。可以像使用正则表达式中任何其他字符一样使用通配符。例如,如果想匹配<code>"hug"</code>、<code>"huh"</code>、<code>"hut"</code>和<code>"hum"</code>,可以使用正则表达式<code>/hu./</code>匹配以上四个单词。
|
||||
# --description--
|
||||
|
||||
有时不(或不需要)知道匹配模式中的确切字符。如果要精确匹配到完整的单词,那出现一个拼写错误就会匹配不到。幸运的是,可以使用通配符`.`来处理这种情况。
|
||||
|
||||
通配符`.`将匹配任何一个字符。通配符也叫`dot`或`period`。可以像使用正则表达式中任何其他字符一样使用通配符。例如,如果想匹配`"hug"`、`"huh"`、`"hut"`和`"hum"`,可以使用正则表达式`/hu./`匹配以上四个单词。
|
||||
|
||||
```js
|
||||
let humStr = "I'll hum a song";
|
||||
@ -18,67 +19,75 @@ huRegex.test(humStr); // Returns true
|
||||
huRegex.test(hugStr); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
完成正则表达式<code>unRegex</code>以匹配字符串<code>"run"</code>、<code>"sun"</code>、<code>"fun"</code>、<code>"pun"</code>、<code>"nun"</code>和<code>"bun"</code>。正则表达式中应该使用通配符。
|
||||
</section>
|
||||
完成正则表达式`unRegex`以匹配字符串`"run"`、`"sun"`、`"fun"`、`"pun"`、`"nun"`和`"bun"`。正则表达式中应该使用通配符。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>.test()</code>方法。
|
||||
testString: assert(code.match(/\.test\(.*\)/));
|
||||
- text: 你应该在你的正则表达式<code>unRegex</code>中使用通配符。
|
||||
testString: assert(/\./.test(unRegex.source));
|
||||
- text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'Let us go on a run.'</code>中匹配到<code>'run'</code>单词。"
|
||||
testString: assert(unRegex.test("Let us go on a run."));
|
||||
- text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'The sun is out today.'</code>中匹配到<code>'sun'</code>单词。"
|
||||
testString: assert(unRegex.test("The sun is out today."));
|
||||
- text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'Coding is a lot of fun.'</code>中匹配到<code>'fun'</code>单词。"
|
||||
testString: assert(unRegex.test("Coding is a lot of fun."));
|
||||
- text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'Seven days without a pun makes one weak.'</code>中匹配到<code>'pun'</code>单词。"
|
||||
testString: assert(unRegex.test("Seven days without a pun makes one weak."));
|
||||
- text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'One takes a vow to be a nun.'</code>中匹配到<code>'nun'</code>单词。"
|
||||
testString: assert(unRegex.test("One takes a vow to be a nun."));
|
||||
- text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'She got fired from the hot dog stand for putting her hair in a bun.'</code>中匹配到<code>'bun'</code>单词。"
|
||||
testString: assert(unRegex.test("She got fired from the hot dog stand for putting her hair in a bun."));
|
||||
- text: "你的正则表达式<code>unRegex</code>不应该匹配<code>'There is a bug in my code.'</code>。"
|
||||
testString: assert(!unRegex.test("There is a bug in my code."));
|
||||
- text: "你的正则表达式<code>unRegex</code>不应该匹配<code>'Catch me if you can.'</code>。"
|
||||
testString: assert(!unRegex.test("Can me if you can."));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该使用`.test()`方法。
|
||||
|
||||
```js
|
||||
let exampleStr = "Let's have fun with regular expressions!";
|
||||
let unRegex = /change/; // Change this line
|
||||
let result = unRegex.test(exampleStr);
|
||||
assert(code.match(/\.test\(.*\)/));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你应该在你的正则表达式`unRegex`中使用通配符。
|
||||
|
||||
```js
|
||||
let exampleStr = "Let's have fun with regular expressions!";
|
||||
let unRegex = /.un/; // Change this line
|
||||
let result = unRegex.test(exampleStr);
|
||||
assert(/\./.test(unRegex.source));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式`unRegex`应该在字符串`'Let us go on a run.'`中匹配到`'run'`单词。
|
||||
|
||||
```js
|
||||
assert(unRegex.test('Let us go on a run.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'The sun is out today.'`中匹配到`'sun'`单词。
|
||||
|
||||
```js
|
||||
assert(unRegex.test('The sun is out today.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'Coding is a lot of fun.'`中匹配到`'fun'`单词。
|
||||
|
||||
```js
|
||||
assert(unRegex.test('Coding is a lot of fun.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'Seven days without a pun makes one weak.'`中匹配到`'pun'`单词。
|
||||
|
||||
```js
|
||||
assert(unRegex.test('Seven days without a pun makes one weak.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`应该在字符串`'One takes a vow to be a nun.'`中匹配到`'nun'`单词。
|
||||
|
||||
```js
|
||||
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'`单词。
|
||||
|
||||
```js
|
||||
assert(
|
||||
unRegex.test(
|
||||
'She got fired from the hot dog stand for putting her hair in a bun.'
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`不应该匹配`'There is a bug in my code.'`。
|
||||
|
||||
```js
|
||||
assert(!unRegex.test('There is a bug in my code.'));
|
||||
```
|
||||
|
||||
你的正则表达式`unRegex`不应该匹配`'Catch me if you can.'`。
|
||||
|
||||
```js
|
||||
assert(!unRegex.test('Can me if you can.'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db7367417b2b2512b9d
|
||||
title: 匹配字符串的开头
|
||||
challengeType: 1
|
||||
forumTopicId: 301349
|
||||
title: 匹配字符串的开头
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
回顾一下之前的挑战,正则表达式可以用于查找多项匹配。还可以查询字符串中符合指定匹配模式的字符。
|
||||
在之前的挑战中,使用<code>字符集</code>中的<code>插入</code>符号(<code>^</code>)来创建一个<code>否定字符集</code>,形如<code>[^thingsThatWillNotBeMatched]</code>。在<code>字符集</code>之外,<code>插入</code>符号用于字符串的开头搜寻匹配模式。
|
||||
|
||||
在之前的挑战中,使用`字符集`中的`插入`符号(`^`)来创建一个`否定字符集`,形如`[^thingsThatWillNotBeMatched]`。在`字符集`之外,`插入`符号用于字符串的开头搜寻匹配模式。
|
||||
|
||||
```js
|
||||
let firstString = "Ricky is first and can be found.";
|
||||
@ -20,55 +21,35 @@ firstRegex.test(notFirst);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在正则表达式中使用<code>^</code>符号,以匹配仅在字符串<code>rickyAndCal</code>的开头出现的<code>"Cal"</code>。
|
||||
</section>
|
||||
在正则表达式中使用`^`符号,以匹配仅在字符串`rickyAndCal`的开头出现的`"Cal"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: "你的正则表达式应该搜寻有一个大写字母的<code>'Cal'</code>。"
|
||||
testString: assert(calRegex.source == "^Cal");
|
||||
- text: 你的正则表达式不应该使用任何标志。
|
||||
testString: assert(calRegex.flags == "");
|
||||
- text: "你的正则表达式应该匹配字符串开头的<code>'Cal'</code>。"
|
||||
testString: assert(calRegex.test("Cal and Ricky both like racing."));
|
||||
- text: "你的正则表达式不应该匹配字符串中间的<code>'Cal'</code>。"
|
||||
testString: assert(!calRegex.test("Ricky and Cal both like racing."));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该搜寻有一个大写字母的`'Cal'`。
|
||||
|
||||
```js
|
||||
let rickyAndCal = "Cal and Ricky both like racing.";
|
||||
let calRegex = /change/; // Change this line
|
||||
let result = calRegex.test(rickyAndCal);
|
||||
assert(calRegex.source == '^Cal');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式不应该使用任何标志。
|
||||
|
||||
```js
|
||||
let rickyAndCal = "Cal and Ricky both like racing.";
|
||||
let calRegex = /^Cal/; // Change this line
|
||||
let result = calRegex.test(rickyAndCal);
|
||||
assert(calRegex.flags == '');
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该匹配字符串开头的`'Cal'`。
|
||||
|
||||
```js
|
||||
assert(calRegex.test('Cal and Ricky both like racing.'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配字符串中间的`'Cal'`。
|
||||
|
||||
```js
|
||||
assert(!calRegex.test('Ricky and Cal both like racing.'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,63 +1,43 @@
|
||||
---
|
||||
id: 587d7db6367417b2b2512b99
|
||||
title: 匹配出现一次或多次的字符
|
||||
challengeType: 1
|
||||
forumTopicId: 301350
|
||||
title: 匹配出现一次或多次的字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
有时,需要匹配出现一次或者连续多次的的字符(或字符组)。这意味着它至少出现一次,并且可能重复出现。
|
||||
可以使用<code>+</code>符号来检查情况是否如此。记住,字符或匹配模式必须一个接一个地连续出现。
|
||||
例如,<code>/a+/g</code>会在<code>"abc"</code>中匹配到一个匹配项,并且返回<code>["a"]</code>。因为<code>+</code>的存在,它也会在<code>"aabc"</code>中匹配到一个匹配项,然后返回<code>["aa"]</code>。
|
||||
如果它是检查字符串<code>"abab"</code>,它将匹配到两个匹配项并且返回<code>["a", "a"]</code>,因为<code>a</code>字符不连续,在它们之间有一个<code>b</code>字符。最后,因为在字符串<code>"bcd"</code>中没有<code>"a"</code>,因此找不到匹配项。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
想要在字符串<code>"Mississippi"</code>中匹配到出现一次或多次的字母<code>s</code>的匹配项。编写一个使用<code>+</code>符号的正则表达式。
|
||||
</section>
|
||||
可以使用`+`符号来检查情况是否如此。记住,字符或匹配模式必须一个接一个地连续出现。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
例如,`/a+/g`会在`"abc"`中匹配到一个匹配项,并且返回`["a"]`。因为`+`的存在,它也会在`"aabc"`中匹配到一个匹配项,然后返回`["aa"]`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>myRegex</code>应该使用<code>+</code>符号来匹配一个或多个<code>s</code>字符。
|
||||
testString: assert(/\+/.test(myRegex.source));
|
||||
- text: 你的正则表达式<code>myRegex</code>应该匹配两项。
|
||||
testString: assert(result.length == 2);
|
||||
- text: "<code>结果</code>变量应该是一个包含两个<code>'ss'</code>匹配项的数组。"
|
||||
testString: assert(result[0] == 'ss' && result[1] == 'ss');
|
||||
如果它是检查字符串`"abab"`,它将匹配到两个匹配项并且返回`["a", "a"]`,因为`a`字符不连续,在它们之间有一个`b`字符。最后,因为在字符串`"bcd"`中没有`"a"`,因此找不到匹配项。
|
||||
|
||||
```
|
||||
# --instructions--
|
||||
|
||||
</section>
|
||||
想要在字符串`"Mississippi"`中匹配到出现一次或多次的字母`s`的匹配项。编写一个使用`+`符号的正则表达式。
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
# --hints--
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式`myRegex`应该使用`+`符号来匹配一个或多个`s`字符。
|
||||
|
||||
```js
|
||||
let difficultSpelling = "Mississippi";
|
||||
let myRegex = /change/; // Change this line
|
||||
let result = difficultSpelling.match(myRegex);
|
||||
assert(/\+/.test(myRegex.source));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式`myRegex`应该匹配两项。
|
||||
|
||||
```js
|
||||
let difficultSpelling = "Mississippi";
|
||||
let myRegex = /s+/g; // Change this line
|
||||
let result = difficultSpelling.match(myRegex);
|
||||
assert(result.length == 2);
|
||||
```
|
||||
|
||||
</section>
|
||||
`结果`变量应该是一个包含两个`'ss'`匹配项的数组。
|
||||
|
||||
```js
|
||||
assert(result[0] == 'ss' && result[1] == 'ss');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db6367417b2b2512b9a
|
||||
title: 匹配出现零次或多次的字符
|
||||
challengeType: 1
|
||||
forumTopicId: 301351
|
||||
title: 匹配出现零次或多次的字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
上一次的挑战中使用了加号<code>+</code>来查找出现一次或多次的字符。还有一个选项可以匹配出现零次或多次的字符。
|
||||
执行该操作的字符叫做<code>asterisk</code>或<code>star</code>,即<code>*</code>。
|
||||
# --description--
|
||||
|
||||
上一次的挑战中使用了加号`+`来查找出现一次或多次的字符。还有一个选项可以匹配出现零次或多次的字符。
|
||||
|
||||
执行该操作的字符叫做`asterisk`或`star`,即`*`。
|
||||
|
||||
```js
|
||||
let soccerWord = "gooooooooal!";
|
||||
@ -20,64 +21,51 @@ gPhrase.match(goRegex); // Returns ["g"]
|
||||
oPhrase.match(goRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在这个挑战里,<code>chewieQuote</code> 已经被初始化为 "Aaaaaaaaaaaaaaaarrrgh!"。创建一个变量为<code>chewieRegex</code>的正则表达式,使用<code>*</code>符号在<code>chewieQuote</code>中匹配<code>"A"</code>及其之后出现的零个或多个<code>"a"</code>。你的正则表达式不需要使用修饰符,也不需要匹配引号。
|
||||
</section>
|
||||
在这个挑战里,`chewieQuote` 已经被初始化为 "Aaaaaaaaaaaaaaaarrrgh!"。创建一个变量为`chewieRegex`的正则表达式,使用`*`符号在`chewieQuote`中匹配`"A"`及其之后出现的零个或多个`"a"`。你的正则表达式不需要使用修饰符,也不需要匹配引号。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: "你的正则表达式<code>chewieRegex</code>应该使用<code>*</code>符号匹配<code>'A'</code>之后出现的零个或多个<code>'a'</code>字符。"
|
||||
testString: assert(/\*/.test(chewieRegex.source));
|
||||
- text: 正则表达式应当匹配 <code>chewieQuote</code> 里的 <code>"A"</code>。
|
||||
testString: assert(result[0][0] === 'A');
|
||||
- text: "你的正则表达式应该匹配<code>'Aaaaaaaaaaaaaaaa'</code>。"
|
||||
testString: assert(result[0] === 'Aaaaaaaaaaaaaaaa');
|
||||
- text: 你的正则表达式<code>chewieRegex</code>应该匹配 16 个字符。
|
||||
testString: assert(result[0].length === 16);
|
||||
- text: "你的正则表达式在<code>'He made a fair move. Screaming about it can't help you.'</code>中不应该匹配任何字符。"
|
||||
testString: assert(!"He made a fair move. Screaming about it can't help you.".match(chewieRegex));
|
||||
- text: "你的正则表达式在<code>'Let him have it. It's not wise to upset a Wookiee.'</code>中不应该匹配任何字符。"
|
||||
testString: assert(!"Let him have it. It's not wise to upset a Wookiee.".match(chewieRegex));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式`chewieRegex`应该使用`*`符号匹配`'A'`之后出现的零个或多个`'a'`字符。
|
||||
|
||||
```js
|
||||
let chewieRegex = /change/; // Only change this line
|
||||
let result = chewieQuote.match(chewieRegex);
|
||||
assert(/\*/.test(chewieRegex.source));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## Before Test
|
||||
<div id='js-setup'>
|
||||
正则表达式应当匹配 `chewieQuote` 里的 `"A"`。
|
||||
|
||||
```js
|
||||
const chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
|
||||
assert(result[0][0] === 'A');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该匹配`'Aaaaaaaaaaaaaaaa'`。
|
||||
|
||||
```js
|
||||
let chewieRegex = /Aa*/;
|
||||
let result = chewieQuote.match(chewieRegex);
|
||||
assert(result[0] === 'Aaaaaaaaaaaaaaaa');
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式`chewieRegex`应该匹配 16 个字符。
|
||||
|
||||
```js
|
||||
assert(result[0].length === 16);
|
||||
```
|
||||
|
||||
你的正则表达式在`'He made a fair move. Screaming about it can't help you.'`中不应该匹配任何字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
!"He made a fair move. Screaming about it can't help you.".match(chewieRegex)
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式在`'Let him have it. It's not wise to upset a Wookiee.'`中不应该匹配任何字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
!"Let him have it. It's not wise to upset a Wookiee.".match(chewieRegex)
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db7367417b2b2512b9e
|
||||
title: 匹配字符串的末尾
|
||||
challengeType: 1
|
||||
forumTopicId: 301352
|
||||
title: 匹配字符串的末尾
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在上一个挑战中,学习了使用<code>^</code>符号来搜寻字符串开头的匹配模式。还有一种方法可以搜寻字符串末尾的匹配模式。
|
||||
可以使用正则表达式的<code>美元</code>符号<code>$</code>来搜寻字符串的结尾。
|
||||
# --description--
|
||||
|
||||
在上一个挑战中,学习了使用`^`符号来搜寻字符串开头的匹配模式。还有一种方法可以搜寻字符串末尾的匹配模式。
|
||||
|
||||
可以使用正则表达式的`美元`符号`$`来搜寻字符串的结尾。
|
||||
|
||||
```js
|
||||
let theEnding = "This is a never ending story";
|
||||
@ -21,53 +22,29 @@ storyRegex.test(noEnding);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>$</code>在字符串<code>caboose</code>的末尾匹配<code>"caboose"</code>。
|
||||
</section>
|
||||
使用`$`在字符串`caboose`的末尾匹配`"caboose"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: "你应该在正则表达式使用美元符号<code>$</code>来搜寻<code>'caboose'</code>。"
|
||||
testString: assert(lastRegex.source == "caboose$");
|
||||
- text: 你的正则表达式不应该使用任何标志。
|
||||
testString: assert(lastRegex.flags == "");
|
||||
- text: "你应该在字符串<code>'The last car on a train is the caboose'</code>的末尾匹配<code>'caboose'</code>。"
|
||||
testString: assert(lastRegex.test("The last car on a train is the caboose"));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该在正则表达式使用美元符号`$`来搜寻`'caboose'`。
|
||||
|
||||
```js
|
||||
let caboose = "The last car on a train is the caboose";
|
||||
let lastRegex = /change/; // Change this line
|
||||
let result = lastRegex.test(caboose);
|
||||
assert(lastRegex.source == 'caboose$');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式不应该使用任何标志。
|
||||
|
||||
```js
|
||||
let caboose = "The last car on a train is the caboose";
|
||||
let lastRegex = /caboose$/; // Change this line
|
||||
let result = lastRegex.test(caboose);
|
||||
assert(lastRegex.flags == '');
|
||||
```
|
||||
|
||||
</section>
|
||||
你应该在字符串`'The last car on a train is the caboose'`的末尾匹配`'caboose'`。
|
||||
|
||||
```js
|
||||
assert(lastRegex.test('The last car on a train is the caboose'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db8367417b2b2512ba0
|
||||
title: 匹配除了字母和数字的所有符号
|
||||
challengeType: 1
|
||||
forumTopicId: 301353
|
||||
title: 匹配除了字母和数字的所有符号
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
已经了解到可以使用缩写<code>\w</code>来匹配字母和数字<code>[A-Za-z0-9_]</code>。不过,有可能想要搜寻的匹配模式是非字母数字字符。
|
||||
可以使用<code>\W</code>搜寻和<code>\w</code>相反的匹配模式。注意,相反匹配模式使用大写字母。此缩写与<code>[^A-Za-z0-9_]</code>是一样的。
|
||||
# --description--
|
||||
|
||||
已经了解到可以使用缩写`\w`来匹配字母和数字`[A-Za-z0-9_]`。不过,有可能想要搜寻的匹配模式是非字母数字字符。
|
||||
|
||||
可以使用`\W`搜寻和`\w`相反的匹配模式。注意,相反匹配模式使用大写字母。此缩写与`[^A-Za-z0-9_]`是一样的。
|
||||
|
||||
```js
|
||||
let shortHand = /\W/;
|
||||
@ -18,59 +19,56 @@ numbers.match(shortHand); // Returns ["%"]
|
||||
sentence.match(shortHand); // Returns ["!"]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用缩写<code>\W</code>来计算不同引号和字符串中非字母数字字符的数量。
|
||||
</section>
|
||||
使用缩写`\W`来计算不同引号和字符串中非字母数字字符的数量。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(nonAlphabetRegex.global);
|
||||
- text: "你的正则表达式应该在<code>'The five boxing wizards jump quickly.'</code>中匹配到 6 个非字母数字字符。"
|
||||
testString: assert("The five boxing wizards jump quickly.".match(nonAlphabetRegex).length == 6);
|
||||
- text: 正则表达式应该使用元字符来匹配非字母字符。
|
||||
testString: assert(/\\W/.test(nonAlphabetRegex.source));
|
||||
- text: "你的正则表达式应该在<code>'Pack my box with five dozen liquor jugs.'</code>中匹配到 8 个非字母数字字符。"
|
||||
testString: assert("Pack my box with five dozen liquor jugs.".match(nonAlphabetRegex).length == 8);
|
||||
- text: "你的正则表达式应该在<code>'How vexingly quick daft zebras jump!'</code>中匹配到 6 个非字母数字字符。"
|
||||
testString: assert("How vexingly quick daft zebras jump!".match(nonAlphabetRegex).length == 6);
|
||||
- text: "你的正则表达式应该在<code>'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'</code>中匹配到 12 个非字母数字字符。"
|
||||
testString: assert("123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.".match(nonAlphabetRegex).length == 12);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
|
||||
```js
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let nonAlphabetRegex = /change/; // Change this line
|
||||
let result = quoteSample.match(nonAlphabetRegex).length;
|
||||
assert(nonAlphabetRegex.global);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该在`'The five boxing wizards jump quickly.'`中匹配到 6 个非字母数字字符。
|
||||
|
||||
```js
|
||||
let quoteSample = "The five boxing wizards_jump quickly.";
|
||||
let nonAlphabetRegex = /\W/g; // Change this line
|
||||
let result = quoteSample.match(nonAlphabetRegex).length;
|
||||
assert(
|
||||
'The five boxing wizards jump quickly.'.match(nonAlphabetRegex).length == 6
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
正则表达式应该使用元字符来匹配非字母字符。
|
||||
|
||||
```js
|
||||
assert(/\\W/.test(nonAlphabetRegex.source));
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'Pack my box with five dozen liquor jugs.'`中匹配到 8 个非字母数字字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'Pack my box with five dozen liquor jugs.'.match(nonAlphabetRegex).length == 8
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'How vexingly quick daft zebras jump!'`中匹配到 6 个非字母数字字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'How vexingly quick daft zebras jump!'.match(nonAlphabetRegex).length == 6
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'`中匹配到 12 个非字母数字字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'.match(nonAlphabetRegex)
|
||||
.length == 12
|
||||
);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 587d7db5367417b2b2512b96
|
||||
title: 匹配字母表中的字母
|
||||
challengeType: 1
|
||||
forumTopicId: 301354
|
||||
title: 匹配字母表中的字母
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
了解了如何使用<code>字符集</code>来指定要匹配的一组字符串,但是当需要匹配大量字符(例如,字母表中的每个字母)时,有一种写法可以让实现这个功能变得简短。
|
||||
在<code>字符集</code>中,可以使用<code>连字符</code>(<code>-</code>)来定义要匹配的字符范围。
|
||||
例如,要匹配小写字母<code>a</code>到<code>e</code>,你可以使用<code>[a-e]</code>。
|
||||
# --description--
|
||||
|
||||
了解了如何使用`字符集`来指定要匹配的一组字符串,但是当需要匹配大量字符(例如,字母表中的每个字母)时,有一种写法可以让实现这个功能变得简短。
|
||||
|
||||
在`字符集`中,可以使用`连字符`(`-`)来定义要匹配的字符范围。
|
||||
|
||||
例如,要匹配小写字母`a`到`e`,你可以使用`[a-e]`。
|
||||
|
||||
```js
|
||||
let catStr = "cat";
|
||||
@ -21,54 +23,32 @@ batStr.match(bgRegex); // Returns ["bat"]
|
||||
matStr.match(bgRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
匹配字符串<code>quoteSample</code>中的所有字母。
|
||||
<strong>注意:</strong><br>一定要同时匹配大小写<strong>字母<strong>。
|
||||
</section>
|
||||
匹配字符串`quoteSample`中的所有字母。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**注意:**
|
||||
一定要同时匹配大小写**字母**。\*\*\*\*
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>alphabetRegex</code>应该匹配 35 项。
|
||||
testString: assert(result.length == 35);
|
||||
- text: 你的正则表达式<code>alphabetRegex</code>应该使用全局标志。
|
||||
testString: assert(alphabetRegex.flags.match(/g/).length == 1);
|
||||
- text: 你的正则表达式<code>alphabetRegex</code>应该使用忽略大小写标志。
|
||||
testString: assert(alphabetRegex.flags.match(/i/).length == 1);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式`alphabetRegex`应该匹配 35 项。
|
||||
|
||||
```js
|
||||
let quoteSample = "The quick brown fox jumps over the lazy dog.";
|
||||
let alphabetRegex = /change/; // Change this line
|
||||
let result = alphabetRegex; // Change this line
|
||||
assert(result.length == 35);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式`alphabetRegex`应该使用全局标志。
|
||||
|
||||
```js
|
||||
let quoteSample = "The quick brown fox jumps over the lazy dog.";
|
||||
let alphabetRegex = /[a-z]/gi; // Change this line
|
||||
let result = quoteSample.match(alphabetRegex); // Change this line
|
||||
assert(alphabetRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式`alphabetRegex`应该使用忽略大小写标志。
|
||||
|
||||
```js
|
||||
assert(alphabetRegex.flags.match(/i/).length == 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
---
|
||||
id: 587d7db3367417b2b2512b8f
|
||||
title: 匹配文字字符串
|
||||
challengeType: 1
|
||||
forumTopicId: 301355
|
||||
title: 匹配文字字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
在上一个挑战中,使用正则表达式<code>/Hello/</code>搜索到了字符串<code>"Hello"</code>。那个正则表达式在字符串中搜寻<code>"Hello"</code>的文字匹配。下面是另一个在字符串中搜寻<code>"Kevin"</code>的示例:
|
||||
# --description--
|
||||
|
||||
在上一个挑战中,使用正则表达式`/Hello/`搜索到了字符串`"Hello"`。那个正则表达式在字符串中搜寻`"Hello"`的文字匹配。下面是另一个在字符串中搜寻`"Kevin"`的示例:
|
||||
|
||||
```js
|
||||
let testStr = "Hello, my name is Kevin.";
|
||||
@ -16,7 +16,7 @@ testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
任何其他形式的<code>"Kevin"</code>都不会被匹配。例如,正则表达式<code>/Kevin/</code>不会匹配<code>"kevin"</code>或者<code>"KEVIN"</code>。
|
||||
任何其他形式的`"Kevin"`都不会被匹配。例如,正则表达式`/Kevin/`不会匹配`"kevin"`或者`"KEVIN"`。
|
||||
|
||||
```js
|
||||
let wrongRegex = /kevin/;
|
||||
@ -25,53 +25,30 @@ wrongRegex.test(testStr);
|
||||
```
|
||||
|
||||
后续的挑战将为你展示如何匹配其他形式的字符串。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
完成正则表达式<code>waldoRegex</code>,在字符串<code>waldoIsHiding</code>中匹配到文本<code>"Waldo"</code>。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
完成正则表达式`waldoRegex`,在字符串`waldoIsHiding`中匹配到文本`"Waldo"`。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: "你的正则表达式<code>waldoRegex</code>应该匹配到<code>'Waldo'</code>。"
|
||||
testString: assert(waldoRegex.test(waldoIsHiding));
|
||||
- text: 你的正则表达式<code>waldoRegex</code>不应该搜寻其他的任何内容。
|
||||
testString: assert(!waldoRegex.test('Somewhere is hiding in this text.'));
|
||||
- text: 你应该使用你的正则表达式对字符串执行文字匹配。
|
||||
testString: assert(!/\/.*\/i/.test(code));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式`waldoRegex`应该匹配到`'Waldo'`。
|
||||
|
||||
```js
|
||||
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
|
||||
let waldoRegex = /search/; // Change this line
|
||||
let result = waldoRegex.test(waldoIsHiding);
|
||||
assert(waldoRegex.test(waldoIsHiding));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式`waldoRegex`不应该搜寻其他的任何内容。
|
||||
|
||||
```js
|
||||
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
|
||||
let waldoRegex = /Waldo/; // Change this line
|
||||
let result = waldoRegex.test(waldoIsHiding);
|
||||
assert(!waldoRegex.test('Somewhere is hiding in this text.'));
|
||||
```
|
||||
|
||||
</section>
|
||||
你应该使用你的正则表达式对字符串执行文字匹配。
|
||||
|
||||
```js
|
||||
assert(!/\/.*\/i/.test(code));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db9367417b2b2512ba4
|
||||
title: 匹配非空白字符
|
||||
challengeType: 1
|
||||
forumTopicId: 18210
|
||||
title: 匹配非空白字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
已经学会了如何使用带有小写<code>s</code>的缩写<code>\s</code>来搜寻空白字符。还可以搜寻除了空格之外的所有内容。
|
||||
使用<code>\S</code>搜寻非空白字符,其中<code>S</code>是大写。此匹配模式将不匹配空格、回车符、制表符、换页符和换行符。可以认为这类似于元字符<code>[^\r\t\f\n\v]</code>。
|
||||
# --description--
|
||||
|
||||
已经学会了如何使用带有小写`s`的缩写`\s`来搜寻空白字符。还可以搜寻除了空格之外的所有内容。
|
||||
|
||||
使用`\S`搜寻非空白字符,其中`S`是大写。此匹配模式将不匹配空格、回车符、制表符、换页符和换行符。可以认为这类似于元字符`[^\r\t\f\n\v]`。
|
||||
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
@ -16,57 +17,44 @@ let nonSpaceRegex = /\S/g;
|
||||
whiteSpace.match(nonSpaceRegex).length; // Returns 32
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>countNonWhiteSpace</code>以查找字符串中的多个非空字符。
|
||||
</section>
|
||||
修改正则表达式`countNonWhiteSpace`以查找字符串中的多个非空字符。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(countNonWhiteSpace.global);
|
||||
- text: 正则表达式应该使用元字符 <code>\S/code> 来匹配所有的非空格字符。
|
||||
testString: assert(/\\S/.test(countNonWhiteSpace.source));
|
||||
- text: "你的正则表达式应该在<code>'Men are from Mars and women are from Venus.'</code>中匹配到 35 个非空白字符。"
|
||||
testString: assert("Men are from Mars and women are from Venus.".match(countNonWhiteSpace).length == 35);
|
||||
- text: '你的正则表达式应该在<code>"Space: the final frontier."</code>中匹配到 23 个非空白字符。'
|
||||
testString: 'assert("Space: the final frontier.".match(countNonWhiteSpace).length == 23);'
|
||||
- text: "你的正则表达式应该在<code>'MindYourPersonalSpace'</code>中匹配到 21 个非空白字符。"
|
||||
testString: assert("MindYourPersonalSpace".match(countNonWhiteSpace).length == 21);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
|
||||
```js
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countNonWhiteSpace = /change/; // Change this line
|
||||
let result = sample.match(countNonWhiteSpace);
|
||||
assert(countNonWhiteSpace.global);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
正则表达式应该使用元字符 <code>\\S/code> 来匹配所有的非空格字符。
|
||||
|
||||
```js
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countNonWhiteSpace = /\S/g; // Change this line
|
||||
let result = sample.match(countNonWhiteSpace);
|
||||
assert(/\\S/.test(countNonWhiteSpace.source));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该在`'Men are from Mars and women are from Venus.'`中匹配到 35 个非空白字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'Men are from Mars and women are from Venus.'.match(countNonWhiteSpace)
|
||||
.length == 35
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`"Space: the final frontier."`中匹配到 23 个非空白字符。
|
||||
|
||||
```js
|
||||
assert('Space: the final frontier.'.match(countNonWhiteSpace).length == 23);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`'MindYourPersonalSpace'`中匹配到 21 个非空白字符。
|
||||
|
||||
```js
|
||||
assert('MindYourPersonalSpace'.match(countNonWhiteSpace).length == 21);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,16 @@
|
||||
---
|
||||
id: 587d7db5367417b2b2512b97
|
||||
title: 匹配字母表中的数字和字母
|
||||
challengeType: 1
|
||||
forumTopicId: 301356
|
||||
title: 匹配字母表中的数字和字母
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
使用连字符(<code>-</code>)匹配字符范围并不仅限于字母。它还可以匹配一系列数字。
|
||||
例如,<code>/[0-5]/</code>匹配<code>0</code>和<code>5</code>之间的任意数字,包含<code>0</code>和<code>5</code>。
|
||||
# --description--
|
||||
|
||||
使用连字符(`-`)匹配字符范围并不仅限于字母。它还可以匹配一系列数字。
|
||||
|
||||
例如,`/[0-5]/`匹配`0`和`5`之间的任意数字,包含`0`和`5`。
|
||||
|
||||
此外,还可以在单个字符集中组合一系列字母和数字。
|
||||
|
||||
```js
|
||||
@ -18,54 +20,29 @@ let myRegex = /[a-z0-9]/ig;
|
||||
jennyStr.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
创建一个正则表达式,使其可以匹配<code>h</code>和<code>s</code>之间的一系列字母,以及<code>2</code>和<code>6</code>之间的一系列数字。请记得在正则表达式中包含恰当的标志。
|
||||
</section>
|
||||
创建一个正则表达式,使其可以匹配`h`和`s`之间的一系列字母,以及`2`和`6`之间的一系列数字。请记得在正则表达式中包含恰当的标志。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>myRegex</code>应该匹配 17 项。
|
||||
testString: assert(result.length == 17);
|
||||
- text: 你的正则表达式<code>myRegex</code>应该使用全局标志。
|
||||
testString: assert(myRegex.flags.match(/g/).length == 1);
|
||||
- text: 你的正则表达式<code>myRegex</code>应该使用忽略大小写的标志。
|
||||
testString: assert(myRegex.flags.match(/i/).length == 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式`myRegex`应该匹配 17 项。
|
||||
|
||||
```js
|
||||
let quoteSample = "Blueberry 3.141592653s are delicious.";
|
||||
let myRegex = /change/; // Change this line
|
||||
let result = myRegex; // Change this line
|
||||
assert(result.length == 17);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式`myRegex`应该使用全局标志。
|
||||
|
||||
```js
|
||||
let quoteSample = "Blueberry 3.141592653s are delicious.";
|
||||
let myRegex = /[h-s2-6]/gi; // Change this line
|
||||
let result = quoteSample.match(myRegex); // Change this line
|
||||
|
||||
assert(myRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式`myRegex`应该使用忽略大小写的标志。
|
||||
|
||||
```js
|
||||
assert(myRegex.flags.match(/i/).length == 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 587d7db5367417b2b2512b95
|
||||
title: 将单个字符与多种可能性匹配
|
||||
challengeType: 1
|
||||
forumTopicId: 301357
|
||||
title: 将单个字符与多种可能性匹配
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
已经了解了文字匹配模式(<code>/literal/</code>)和通配符(<code>/./</code>)。这是正则表达式的两种极端情况,一种是精确匹配,而另一种则是匹配所有。在这两种极端情况之间有一个平衡选项。
|
||||
可以使用<code>字符集</code>搜寻具有一定灵活性的文字匹配模式。可以把字符集放在方括号(<code>[</code>和<code>]</code>)之间来定义一组需要匹配的字符串。
|
||||
例如,如果想要匹配<code>"bag"</code>、<code>"big"</code>和<code>"bug"</code>,但是不想匹配<code>"bog"</code>。可以创建正则表达式<code>/b[aiu]g/</code>来执行此操作。<code>[aiu]</code>是只匹配字符<code>"a"</code>、<code>"i"</code>或者<code>"u"</code>的字符集。
|
||||
# --description--
|
||||
|
||||
已经了解了文字匹配模式(`/literal/`)和通配符(`/./`)。这是正则表达式的两种极端情况,一种是精确匹配,而另一种则是匹配所有。在这两种极端情况之间有一个平衡选项。
|
||||
|
||||
可以使用`字符集`搜寻具有一定灵活性的文字匹配模式。可以把字符集放在方括号(`[`和`]`)之间来定义一组需要匹配的字符串。
|
||||
|
||||
例如,如果想要匹配`"bag"`、`"big"`和`"bug"`,但是不想匹配`"bog"`。可以创建正则表达式`/b[aiu]g/`来执行此操作。`[aiu]`是只匹配字符`"a"`、`"i"`或者`"u"`的字符集。
|
||||
|
||||
```js
|
||||
let bigStr = "big";
|
||||
@ -23,58 +25,44 @@ bugStr.match(bgRegex); // Returns ["bug"]
|
||||
bogStr.match(bgRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用元音字符集(<code>a</code>、<code>e</code>、<code>i</code>、<code>o</code>、<code>u</code>)在正则表达式<code>vowelRegex</code>中匹配到字符串<code>quoteSample</code>中的所有元音。
|
||||
<strong>注意</strong><br>一定要同时匹配大小写元音。
|
||||
</section>
|
||||
使用元音字符集(`a`、`e`、`i`、`o`、`u`)在正则表达式`vowelRegex`中匹配到字符串`quoteSample`中的所有元音。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**注意**
|
||||
一定要同时匹配大小写元音。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该匹配到所有25个元音。
|
||||
testString: assert(result.length == 25);
|
||||
- text: 你的正则表达式<code>vowelRegex</code>应该使用字符集。
|
||||
testString: assert(/\[.*\]/.test(vowelRegex.source));
|
||||
- text: 你的正则表达式<code>vowelRegex</code>应该使用全局标志。
|
||||
testString: assert(vowelRegex.flags.match(/g/).length == 1);
|
||||
- text: 你的正则表达式<code>vowelRegex</code>应该使用忽略大小写标志。
|
||||
testString: assert(vowelRegex.flags.match(/i/).length == 1);
|
||||
- text: 你的正则表达式不应该匹配任何辅音。
|
||||
testString: assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该匹配到所有25个元音。
|
||||
|
||||
```js
|
||||
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
|
||||
let vowelRegex = /change/; // Change this line
|
||||
let result = vowelRegex; // Change this line
|
||||
assert(result.length == 25);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式`vowelRegex`应该使用字符集。
|
||||
|
||||
```js
|
||||
let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
|
||||
let vowelRegex = /[aeiou]/gi; // Change this line
|
||||
let result = quoteSample.match(vowelRegex); // Change this line
|
||||
assert(/\[.*\]/.test(vowelRegex.source));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式`vowelRegex`应该使用全局标志。
|
||||
|
||||
```js
|
||||
assert(vowelRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式`vowelRegex`应该使用忽略大小写标志。
|
||||
|
||||
```js
|
||||
assert(vowelRegex.flags.match(/i/).length == 1);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配任何辅音。
|
||||
|
||||
```js
|
||||
assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,62 +1,41 @@
|
||||
---
|
||||
id: 587d7db6367417b2b2512b98
|
||||
title: 匹配单个未指定的字符
|
||||
challengeType: 1
|
||||
forumTopicId: 301358
|
||||
title: 匹配单个未指定的字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
到目前为止,已经创建了一个想要匹配的字符集合,但也可以创建一个不想匹配的字符集合。这些类型的字符集称为<code>否定字符集</code>。
|
||||
要创建<code>否定字符集</code>,需要在开始括号后面和不想匹配的字符前面放置<code>插入字符</code>(即<code>^</code>)。
|
||||
例如,<code>/[^aeiou]/gi</code>匹配所有非元音字符。注意,字符<code>.</code>、<code>!</code>、<code>[</code>、<code>@</code>、<code>/</code>和空白字符等也会被匹配,该否定字符集仅排除元音字符。
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
到目前为止,已经创建了一个想要匹配的字符集合,但也可以创建一个不想匹配的字符集合。这些类型的字符集称为`否定字符集`。
|
||||
|
||||
要创建`否定字符集`,需要在开始括号后面和不想匹配的字符前面放置`插入字符`(即`^`)。
|
||||
|
||||
例如,`/[^aeiou]/gi`匹配所有非元音字符。注意,字符`.`、`!`、`[`、`@`、`/`和空白字符等也会被匹配,该否定字符集仅排除元音字符。
|
||||
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
创建一个匹配所有非数字或元音字符的正则表达式。请记得在正则表达式中包含恰当的标志。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>myRegex</code>应该匹配 9 项。
|
||||
testString: assert(result.length == 9);
|
||||
- text: 你的正则表达式<code>myRegex</code>应该使用全局标志。
|
||||
testString: assert(myRegex.flags.match(/g/).length == 1);
|
||||
- text: 你的正则表达式<code>myRegex</code>应该使用忽略大小写标志。
|
||||
testString: assert(myRegex.flags.match(/i/).length == 1);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式`myRegex`应该匹配 9 项。
|
||||
|
||||
```js
|
||||
let quoteSample = "3 blind mice.";
|
||||
let myRegex = /change/; // Change this line
|
||||
let result = myRegex; // Change this line
|
||||
assert(result.length == 9);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式`myRegex`应该使用全局标志。
|
||||
|
||||
```js
|
||||
let quoteSample = "3 blind mice.";
|
||||
let myRegex = /[^0-9aeiou]/gi; // Change this line
|
||||
let result = quoteSample.match(myRegex); // Change this line
|
||||
assert(myRegex.flags.match(/g/).length == 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式`myRegex`应该使用忽略大小写标志。
|
||||
|
||||
```js
|
||||
assert(myRegex.flags.match(/i/).length == 1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7db8367417b2b2512ba3
|
||||
title: 匹配空白字符
|
||||
challengeType: 1
|
||||
forumTopicId: 301359
|
||||
title: 匹配空白字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
迄今为止的挑战包括匹配的字母和数字。还可以匹配字母之间的空格。
|
||||
可以使用<code>\s</code>搜寻空格,其中<code>s</code>是小写。此匹配模式不仅匹配空格,还匹配回车符、制表符、换页符和换行符,可以将其视为与<code>[\r\t\f\n\v]</code>类似。
|
||||
|
||||
可以使用`\s`搜寻空格,其中`s`是小写。此匹配模式不仅匹配空格,还匹配回车符、制表符、换页符和换行符,可以将其视为与`[\r\t\f\n\v]`类似。
|
||||
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
@ -17,57 +18,44 @@ whiteSpace.match(spaceRegex);
|
||||
// Returns [" ", " "]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>countWhiteSpace</code>查找字符串中的多个空白字符。
|
||||
</section>
|
||||
修改正则表达式`countWhiteSpace`查找字符串中的多个空白字符。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(countWhiteSpace.global);
|
||||
- text: 正则表达式应该使用元字符 <code>\s</code> 匹配所有的空白。
|
||||
testString: assert(/\\s/.test(countWhiteSpace.source));
|
||||
- text: "你的正则表达式应该在<code>'Men are from Mars and women are from Venus.'</code>中匹配到 8 个空白字符。"
|
||||
testString: assert("Men are from Mars and women are from Venus.".match(countWhiteSpace).length == 8);
|
||||
- text: '你的正则表达式应该在<code>"Space: the final frontier."</code>中匹配到 3 个空白字符。'
|
||||
testString: 'assert("Space: the final frontier.".match(countWhiteSpace).length == 3);'
|
||||
- text: "你的正则表达式在<code>'MindYourPersonalSpace'</code>中应该匹配不到空白字符。"
|
||||
testString: assert("MindYourPersonalSpace".match(countWhiteSpace) == null);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用全局状态修正符。
|
||||
|
||||
```js
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countWhiteSpace = /change/; // Change this line
|
||||
let result = sample.match(countWhiteSpace);
|
||||
assert(countWhiteSpace.global);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
正则表达式应该使用元字符 `\s` 匹配所有的空白。
|
||||
|
||||
```js
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countWhiteSpace = /\s/g;
|
||||
let result = sample.match(countWhiteSpace);
|
||||
assert(/\\s/.test(countWhiteSpace.source));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该在`'Men are from Mars and women are from Venus.'`中匹配到 8 个空白字符。
|
||||
|
||||
```js
|
||||
assert(
|
||||
'Men are from Mars and women are from Venus.'.match(countWhiteSpace).length ==
|
||||
8
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该在`"Space: the final frontier."`中匹配到 3 个空白字符。
|
||||
|
||||
```js
|
||||
assert('Space: the final frontier.'.match(countWhiteSpace).length == 3);
|
||||
```
|
||||
|
||||
你的正则表达式在`'MindYourPersonalSpace'`中应该匹配不到空白字符。
|
||||
|
||||
```js
|
||||
assert('MindYourPersonalSpace'.match(countWhiteSpace) == null);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,16 +1,20 @@
|
||||
---
|
||||
id: 587d7dba367417b2b2512ba9
|
||||
title: 正向先行断言和负向先行断言
|
||||
challengeType: 1
|
||||
forumTopicId: 301360
|
||||
title: 正向先行断言和负向先行断言
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>先行断言</code>是告诉 JavaScript 在字符串中向前查找的匹配模式。当想要在同一个字符串上搜寻多个匹配模式时,这可能非常有用。
|
||||
有两种<code>先行断言</code>:<code>正向先行断言</code>和<code>负向先行断言</code>。
|
||||
<code>正向先行断言</code>会查看并确保搜索匹配模式中的元素存在,但实际上并不匹配。正向先行断言的用法是<code>(?=...)</code>,其中<code>...</code>就是需要存在但不会被匹配的部分。
|
||||
另一方面,<code>负向先行断言</code>会查看并确保搜索匹配模式中的元素不存在。负向先行断言的用法是<code>(?!...)</code>,其中<code>...</code>是希望不存在的匹配模式。如果负向先行断言部分不存在,将返回匹配模式的其余部分。
|
||||
# --description--
|
||||
|
||||
`先行断言`是告诉 JavaScript 在字符串中向前查找的匹配模式。当想要在同一个字符串上搜寻多个匹配模式时,这可能非常有用。
|
||||
|
||||
有两种`先行断言`:`正向先行断言`和`负向先行断言`。
|
||||
|
||||
`正向先行断言`会查看并确保搜索匹配模式中的元素存在,但实际上并不匹配。正向先行断言的用法是`(?=...)`,其中`...`就是需要存在但不会被匹配的部分。
|
||||
|
||||
另一方面,`负向先行断言`会查看并确保搜索匹配模式中的元素不存在。负向先行断言的用法是`(?!...)`,其中`...`是希望不存在的匹配模式。如果负向先行断言部分不存在,将返回匹配模式的其余部分。
|
||||
|
||||
尽管先行断言有点儿令人困惑,但是这些示例会有所帮助。
|
||||
|
||||
```js
|
||||
@ -22,7 +26,7 @@ quit.match(quRegex); // Returns ["q"]
|
||||
noquit.match(qRegex); // Returns ["q"]
|
||||
```
|
||||
|
||||
<code>先行断言</code>的更实际用途是检查一个字符串中的两个或更多匹配模式。这里有一个简单的密码检查器,密码规则是 3 到 6 个字符且至少包含一个数字:
|
||||
`先行断言`的更实际用途是检查一个字符串中的两个或更多匹配模式。这里有一个简单的密码检查器,密码规则是 3 到 6 个字符且至少包含一个数字:
|
||||
|
||||
```js
|
||||
let password = "abc123";
|
||||
@ -30,68 +34,71 @@ let checkPass = /(?=\w{3,6})(?=\D*\d)/;
|
||||
checkPass.test(password); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在正则表达式<code>pwRegex</code>中使用<code>先行断言</code>以匹配大于5个字符且有两个连续数字的密码,并且不能以数字开头。
|
||||
</section>
|
||||
在正则表达式`pwRegex`中使用`先行断言`以匹配大于5个字符且有两个连续数字的密码,并且不能以数字开头。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用两个正向<code>先行断言</code>。
|
||||
testString: assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
|
||||
- text: "你的正则表达式不应该匹配<code>'astronaut'</code>。"
|
||||
testString: assert(!pwRegex.test("astronaut"));
|
||||
- text: "你的正则表达式不应该匹配<code>'airplanes'</code>。"
|
||||
testString: assert(!pwRegex.test("airplanes"));
|
||||
- text: 正则不应该匹配 <code>"banan1"</code>
|
||||
testString: assert(!pwRegex.test("banan1"));
|
||||
- text: "你的正则表达式应该匹配<code>'bana12'</code>。"
|
||||
testString: assert(pwRegex.test("bana12"));
|
||||
- text: "你的正则表达式应该匹配<code>'abc123'</code>。"
|
||||
testString: assert(pwRegex.test("abc123"));
|
||||
- text: "你的正则表达式不应该匹配<code>'123'</code>。"
|
||||
testString: assert(!pwRegex.test("123"));
|
||||
- text: "你的正则表达式不应该匹配<code>'1234'</code>。"
|
||||
testString: assert(!pwRegex.test("1234"));
|
||||
- text: 正则不应该匹配 <code>"8pass99"</code>
|
||||
testString: assert(!pwRegex.test("8pass99"));
|
||||
- text: 正则不应该匹配 <code>"12abcde"</code>
|
||||
testString: assert(!pwRegex.test("12abcde"));
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用两个正向`先行断言`。
|
||||
|
||||
```js
|
||||
let sampleWord = "astronaut";
|
||||
let pwRegex = /change/; // Change this line
|
||||
let result = pwRegex.test(sampleWord);
|
||||
assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
你的正则表达式不应该匹配`'astronaut'`。
|
||||
|
||||
```js
|
||||
var pwRegex = /^(?=\w{6})(?=\D+\d{2})/;
|
||||
assert(!pwRegex.test('astronaut'));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式不应该匹配`'airplanes'`。
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('airplanes'));
|
||||
```
|
||||
|
||||
正则不应该匹配 `"banan1"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('banan1'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'bana12'`。
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('bana12'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'abc123'`。
|
||||
|
||||
```js
|
||||
assert(pwRegex.test('abc123'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'123'`。
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('123'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'1234'`。
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('1234'));
|
||||
```
|
||||
|
||||
正则不应该匹配 `"8pass99"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('8pass99'));
|
||||
```
|
||||
|
||||
正则不应该匹配 `"12abcde"`
|
||||
|
||||
```js
|
||||
assert(!pwRegex.test('12abcde'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,61 +1,40 @@
|
||||
---
|
||||
id: 587d7dbb367417b2b2512bac
|
||||
title: 删除开头和结尾的空白
|
||||
challengeType: 1
|
||||
forumTopicId: 301362
|
||||
title: 删除开头和结尾的空白
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
有时字符串周围存在的空白字符并不是必需的。字符串的典型处理是删除字符串开头和结尾处的空格。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
# --instructions--
|
||||
|
||||
编写一个正则表达式并使用适当的字符串方法删除字符串开头和结尾的空格。
|
||||
<strong>注意:</strong><br><code>.trim()</code>方法在这里也可以实现同样的效果,但是你需要使用正则表达式来完成此项挑战。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**注意:**
|
||||
`.trim()`方法在这里也可以实现同样的效果,但是你需要使用正则表达式来完成此项挑战。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: "<code>结果</code>应该等于<code>'Hello, World!'</code>。"
|
||||
testString: assert(result == "Hello, World!");
|
||||
- text: 你不应该使用<code>.trim()</code>方法。
|
||||
testString: assert(!code.match(/\.trim\(.*?\)/));
|
||||
- text: <code>结果</code>变量不应该设置为等于字符串。
|
||||
testString: assert(!code.match(/result\s*=\s*".*?"/));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`结果`应该等于`'Hello, World!'`。
|
||||
|
||||
```js
|
||||
let hello = " Hello, World! ";
|
||||
let wsRegex = /change/; // Change this line
|
||||
let result = hello; // Change this line
|
||||
assert(result == 'Hello, World!');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你不应该使用`.trim()`方法。
|
||||
|
||||
```js
|
||||
let hello = " Hello, World! ";
|
||||
let wsRegex = /^(\s+)(.+[^\s])(\s+)$/;
|
||||
let result = hello.replace(wsRegex, '$2');
|
||||
assert(!code.match(/\.trim\(.*?\)/));
|
||||
```
|
||||
|
||||
</section>
|
||||
`结果`变量不应该设置为等于字符串。
|
||||
|
||||
```js
|
||||
assert(!code.match(/result\s*=\s*".*?"/));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,81 +1,95 @@
|
||||
---
|
||||
id: 587d7db8367417b2b2512ba2
|
||||
title: 限制可能的用户名
|
||||
challengeType: 1
|
||||
forumTopicId: 301363
|
||||
title: 限制可能的用户名
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
用户名在互联网上随处可见。它们是用户在自己喜欢的网站上的唯一身份。
|
||||
|
||||
需要检索数据库中的所有用户名。以下是用户在创建用户名时必须遵守的一些简单规则。
|
||||
|
||||
1) 用户名只能是数字字母字符。
|
||||
|
||||
2) 用户名中的数字必须在最后,且数字可以有零个或多个。
|
||||
|
||||
3) 用户名字母可以是小写字母和大写字母。
|
||||
|
||||
4) 用户名长度必须至少为两个字符。两位用户名只能使用字母。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>userCheck</code>以适合上面列出的约束。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
修改正则表达式`userCheck`以适合上面列出的约束。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该匹配<code>JACK</code>。
|
||||
testString: assert(userCheck.test("JACK"));
|
||||
- text: 你的正则表达式不应该匹配<code>J</code>。
|
||||
testString: assert(!userCheck.test("J"));
|
||||
- text: 正则表达式应该匹配 <code>Jo</code>。
|
||||
testString: assert(userCheck.test("Jo"));
|
||||
- text: 你的正则表达式应该匹配<code>Oceans11</code>。
|
||||
testString: assert(userCheck.test("Oceans11"));
|
||||
- text: 你的正则表达式应该匹配<code>RegexGuru</code>。
|
||||
testString: assert(userCheck.test("RegexGuru"));
|
||||
- text: 你的正则表达式不应该匹配<code>007</code>。
|
||||
testString: assert(!userCheck.test("007"));
|
||||
- text: 你的正则表达式不应该匹配<code>9</code>。
|
||||
testString: assert(!userCheck.test("9"));
|
||||
- text: 正则表达式不应该匹配 <code>A1</code>。
|
||||
testString: assert(!userCheck.test("A1"));
|
||||
- text: 正则表达式不应该匹配 <code>BadUs3rnam3</code>。
|
||||
testString: assert(!userCheck.test("BadUs3rnam3"));
|
||||
- text: 正则表达式应该匹配 <code>Z97</code>。
|
||||
testString: assert(userCheck.test("Z97"));
|
||||
- text: 正则表达式不应该匹配 <code>c57bT3</code>。
|
||||
testString: assert(!userCheck.test("c57bT3"));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该匹配`JACK`。
|
||||
|
||||
```js
|
||||
let username = "JackOfAllTrades";
|
||||
let userCheck = /change/; // Change this line
|
||||
let result = userCheck.test(username);
|
||||
assert(userCheck.test('JACK'));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式不应该匹配`J`。
|
||||
|
||||
```js
|
||||
let username = "JackOfAllTrades";
|
||||
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
|
||||
let result = userCheck.test(username);
|
||||
assert(!userCheck.test('J'));
|
||||
```
|
||||
|
||||
</section>
|
||||
正则表达式应该匹配 `Jo`。
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Jo'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`Oceans11`。
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Oceans11'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`RegexGuru`。
|
||||
|
||||
```js
|
||||
assert(userCheck.test('RegexGuru'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`007`。
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('007'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`9`。
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('9'));
|
||||
```
|
||||
|
||||
正则表达式不应该匹配 `A1`。
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('A1'));
|
||||
```
|
||||
|
||||
正则表达式不应该匹配 `BadUs3rnam3`。
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('BadUs3rnam3'));
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `Z97`。
|
||||
|
||||
```js
|
||||
assert(userCheck.test('Z97'));
|
||||
```
|
||||
|
||||
正则表达式不应该匹配 `c57bT3`。
|
||||
|
||||
```js
|
||||
assert(!userCheck.test('c57bT3'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,18 @@
|
||||
---
|
||||
id: 587d7dbb367417b2b2512baa
|
||||
title: 使用捕获组重用模式
|
||||
challengeType: 1
|
||||
forumTopicId: 301364
|
||||
title: 使用捕获组重用模式
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
一些你所搜寻的匹配模式会在字符串中出现多次,手动重复该正则表达式太浪费了。有一种更好的方法可以指定何时在字符串中会有多个重复的子字符串。
|
||||
可以使用<code>捕获组</code>搜寻重复的子字符串。括号<code>(</code>和<code>)</code>可以用来匹配重复的子字符串。只需要把重复匹配模式的正则表达式放在括号中即可。
|
||||
要指定重复字符串将出现的位置,可以使用反斜杠(<code>\</code>)后接一个数字。这个数字从 1 开始,随着你使用的每个捕获组的增加而增加。这里有一个示例,<code>\1</code>可以匹配第一个组。
|
||||
|
||||
可以使用`捕获组`搜寻重复的子字符串。括号`(`和`)`可以用来匹配重复的子字符串。只需要把重复匹配模式的正则表达式放在括号中即可。
|
||||
|
||||
要指定重复字符串将出现的位置,可以使用反斜杠(`\`)后接一个数字。这个数字从 1 开始,随着你使用的每个捕获组的增加而增加。这里有一个示例,`\1`可以匹配第一个组。
|
||||
|
||||
下面的示例匹配任意两个被空格分割的单词:
|
||||
|
||||
```js
|
||||
@ -19,68 +22,76 @@ repeatRegex.test(repeatStr); // Returns true
|
||||
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
|
||||
```
|
||||
|
||||
在字符串上使用<code>.match()</code>方法将返回一个数组,其中包含它匹配的字符串及其捕获组。
|
||||
</section>
|
||||
在字符串上使用`.match()`方法将返回一个数组,其中包含它匹配的字符串及其捕获组。
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
在正则表达式<code>reRegex</code>中使用<code>捕获组</code>,以匹配在字符串中仅重复三次的数字,每一个都由空格分隔。
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
在正则表达式`reRegex`中使用`捕获组`,以匹配在字符串中仅重复三次的数字,每一个都由空格分隔。
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用数字的速记元字符。
|
||||
testString: assert(reRegex.source.match(/\\d/));
|
||||
- text: 你的正则表达式应该重用两次捕获组。
|
||||
testString: assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
||||
- text: 你的正则表达式应该有两个空格分隔这三个数字。
|
||||
testString: assert(reRegex.source.match(/ |\\s/g).length === 2 || reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g));
|
||||
- text: "你的正则表达式应该匹配<code>'42 42 42'</code>。"
|
||||
testString: assert(reRegex.test("42 42 42"));
|
||||
- text: "你的正则表达式应该匹配<code>'100 100 100'</code>。"
|
||||
testString: assert(reRegex.test("100 100 100"));
|
||||
- text: "你的正则表达式不应该匹配<code>'42 42 42 42'</code>。"
|
||||
testString: assert.equal(("42 42 42 42").match(reRegex.source), null);
|
||||
- text: "你的正则表达式不应该匹配<code>'42 42'</code>。"
|
||||
testString: assert.equal(("42 42").match(reRegex.source), null);
|
||||
- text: "你的正则表达式不应该匹配<code>'101 102 103'</code>。"
|
||||
testString: assert(!reRegex.test("101 102 103"));
|
||||
- text: "你的正则表达式不应该匹配<code>'1 2 3'</code>。"
|
||||
testString: assert(!reRegex.test("1 2 3"));
|
||||
- text: "你的正则表达式应该匹配<code>'10 10 10'</code>。"
|
||||
testString: assert(reRegex.test("10 10 10"));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用数字的速记元字符。
|
||||
|
||||
```js
|
||||
let repeatNum = "42 42 42";
|
||||
let reRegex = /change/; // Change this line
|
||||
let result = reRegex.test(repeatNum);
|
||||
assert(reRegex.source.match(/\\d/));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该重用两次捕获组。
|
||||
|
||||
```js
|
||||
let repeatNum = "42 42 42";
|
||||
let reRegex = /^(\d+)\s\1\s\1$/;
|
||||
let result = reRegex.test(repeatNum);
|
||||
assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该有两个空格分隔这三个数字。
|
||||
|
||||
```js
|
||||
assert(
|
||||
reRegex.source.match(/ |\\s/g).length === 2 ||
|
||||
reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g)
|
||||
);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'42 42 42'`。
|
||||
|
||||
```js
|
||||
assert(reRegex.test('42 42 42'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'100 100 100'`。
|
||||
|
||||
```js
|
||||
assert(reRegex.test('100 100 100'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'42 42 42 42'`。
|
||||
|
||||
```js
|
||||
assert.equal('42 42 42 42'.match(reRegex.source), null);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'42 42'`。
|
||||
|
||||
```js
|
||||
assert.equal('42 42'.match(reRegex.source), null);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'101 102 103'`。
|
||||
|
||||
```js
|
||||
assert(!reRegex.test('101 102 103'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'1 2 3'`。
|
||||
|
||||
```js
|
||||
assert(!reRegex.test('1 2 3'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'10 10 10'`。
|
||||
|
||||
```js
|
||||
assert(reRegex.test('10 10 10'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 587d7db9367417b2b2512ba7
|
||||
title: 指定匹配的确切数量
|
||||
challengeType: 1
|
||||
forumTopicId: 301365
|
||||
title: 指定匹配的确切数量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
可以使用带有花括号的<code>数量说明符</code>来指定匹配模式的上下限。但有时只需要特定数量的匹配。
|
||||
# --description--
|
||||
|
||||
可以使用带有花括号的`数量说明符`来指定匹配模式的上下限。但有时只需要特定数量的匹配。
|
||||
|
||||
要指定一定数量的匹配模式,只需在大括号之间放置一个数字。
|
||||
例如,要只匹配字母<code>a</code>出现<code>3</code>次的单词<code>"hah"</code>,正则表达式应为<code>/ha{3}h/</code>。
|
||||
|
||||
例如,要只匹配字母`a`出现`3`次的单词`"hah"`,正则表达式应为`/ha{3}h/`。
|
||||
|
||||
```js
|
||||
let A4 = "haaaah";
|
||||
@ -21,59 +23,47 @@ multipleHA.test(A3); // Returns true
|
||||
multipleHA.test(A100); // Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>timRegex</code>,以匹配仅有四个字母单词<code>m</code>的单词<code>"Timber"</code>。
|
||||
</section>
|
||||
修改正则表达式`timRegex`,以匹配仅有四个字母单词`m`的单词`"Timber"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用花括号。
|
||||
testString: assert(timRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: "你的正则表达式不应该匹配<code>'Timber'</code>。"
|
||||
testString: assert(!timRegex.test("Timber"));
|
||||
- text: "你的正则表达式不应该匹配<code>'Timmber'</code>。"
|
||||
testString: assert(!timRegex.test("Timmber"));
|
||||
- text: "你的正则表达式不应该匹配<code>'Timmmber'</code>。"
|
||||
testString: assert(!timRegex.test("Timmmber"));
|
||||
- text: "你的正则表达式应该匹配<code>'Timmmmber'</code>。"
|
||||
testString: assert(timRegex.test("Timmmmber"));
|
||||
- text: "你的正则表达式不应该匹配包含 30 个字母<code>m</code>的<code>'Timber'</code>。"
|
||||
testString: assert(!timRegex.test("Ti" + "m".repeat(30) + "ber"));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用花括号。
|
||||
|
||||
```js
|
||||
let timStr = "Timmmmber";
|
||||
let timRegex = /change/; // Change this line
|
||||
let result = timRegex.test(timStr);
|
||||
assert(timRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式不应该匹配`'Timber'`。
|
||||
|
||||
```js
|
||||
let timStr = "Timmmmber";
|
||||
let timRegex = /Tim{4}ber/; // Change this line
|
||||
let result = timRegex.test(timStr);
|
||||
assert(!timRegex.test('Timber'));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式不应该匹配`'Timmber'`。
|
||||
|
||||
```js
|
||||
assert(!timRegex.test('Timmber'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Timmmber'`。
|
||||
|
||||
```js
|
||||
assert(!timRegex.test('Timmmber'));
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Timmmmber'`。
|
||||
|
||||
```js
|
||||
assert(timRegex.test('Timmmmber'));
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配包含 30 个字母`m`的`'Timber'`。
|
||||
|
||||
```js
|
||||
assert(!timRegex.test('Ti' + 'm'.repeat(30) + 'ber'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 587d7db9367417b2b2512ba6
|
||||
title: 只指定匹配的下限
|
||||
challengeType: 1
|
||||
forumTopicId: 301366
|
||||
title: 只指定匹配的下限
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
可以使用带有花括号的<code>数量说明符</code>来指定匹配模式的上下限。但有时候只想指定匹配模式的下限而不需要指定上限。
|
||||
# --description--
|
||||
|
||||
可以使用带有花括号的`数量说明符`来指定匹配模式的上下限。但有时候只想指定匹配模式的下限而不需要指定上限。
|
||||
|
||||
为此,在第一个数字后面跟一个逗号即可。
|
||||
例如,要匹配至少出现<code>3</code>次字母<code>a</code>的字符串<code>"hah"</code>,正则表达式应该是<code>/ha{3,}h/</code>。
|
||||
|
||||
例如,要匹配至少出现`3`次字母`a`的字符串`"hah"`,正则表达式应该是`/ha{3,}h/`。
|
||||
|
||||
```js
|
||||
let A4 = "haaaah";
|
||||
@ -21,61 +23,53 @@ multipleA.test(A2); // Returns false
|
||||
multipleA.test(A100); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>haRegex</code>,匹配包含四个或更多字母<code>z</code>的单词<code>"Hazzah"</code>。
|
||||
</section>
|
||||
修改正则表达式`haRegex`,匹配包含四个或更多字母`z`的单词`"Hazzah"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用花括号。
|
||||
testString: assert(haRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: "你的正则表达式不应该匹配<code>'Hazzah'</code>。"
|
||||
testString: assert(!haRegex.test("Hazzah"));
|
||||
- text: "你的正则表达式不应该匹配<code>'Hazzzah'</code>。"
|
||||
testString: assert(!haRegex.test("Hazzzah"));
|
||||
- text: 正则表达式应该匹配 <code>"Hazzzzah"</code>
|
||||
testString: assert("Hazzzzah".match(haRegex)[0].length === 8);
|
||||
- text: "你的正则表达式应该匹配<code>'Hazzzzah'</code>。"
|
||||
testString: assert("Hazzzzzah".match(haRegex)[0].length === 9);
|
||||
- text: 正则表达式应该匹配 <code>"Hazzzzzzah"</code>
|
||||
testString: assert("Hazzzzzzah".match(haRegex)[0].length === 10);
|
||||
- text: 正则表达式应该匹配 <code>"Hazzah"</code> with 30 <code>z</code>'s in it.
|
||||
testString: assert("Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah".match(haRegex)[0].length === 34);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用花括号。
|
||||
|
||||
```js
|
||||
let haStr = "Hazzzzah";
|
||||
let haRegex = /change/; // Change this line
|
||||
let result = haRegex.test(haStr);
|
||||
assert(haRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式不应该匹配`'Hazzah'`。
|
||||
|
||||
```js
|
||||
let haStr = "Hazzzzah";
|
||||
let haRegex = /Haz{4,}ah/; // Change this line
|
||||
let result = haRegex.test(haStr);
|
||||
assert(!haRegex.test('Hazzah'));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式不应该匹配`'Hazzzah'`。
|
||||
|
||||
```js
|
||||
assert(!haRegex.test('Hazzzah'));
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `"Hazzzzah"`
|
||||
|
||||
```js
|
||||
assert('Hazzzzah'.match(haRegex)[0].length === 8);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Hazzzzah'`。
|
||||
|
||||
```js
|
||||
assert('Hazzzzzah'.match(haRegex)[0].length === 9);
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `"Hazzzzzzah"`
|
||||
|
||||
```js
|
||||
assert('Hazzzzzzah'.match(haRegex)[0].length === 10);
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `"Hazzah"` with 30 `z`'s in it.
|
||||
|
||||
```js
|
||||
assert('Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah'.match(haRegex)[0].length === 34);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 587d7db9367417b2b2512ba5
|
||||
title: 指定匹配的上限和下限
|
||||
challengeType: 1
|
||||
forumTopicId: 301367
|
||||
title: 指定匹配的上限和下限
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
回想一下,使用加号<code>+</code>查找一个或多个字符,使用星号<code>*</code>查找零个或多个字符。这些都很方便,但有时需要匹配一定范围的匹配模式。
|
||||
可以使用<code>数量说明符</code>指定匹配模式的上下限。数量说明符与花括号(<code>{</code>和<code>}</code>)一起使用。可以在花括号之间放两个数字,这两个数字代表匹配模式的上限和下限。
|
||||
例如,要在字符串<code>"ah"</code>中匹配仅出现<code>3</code>到<code>5</code>次的字母<code>a</code>,正则表达式应为<code>/a{3,5}h/</code>。
|
||||
# --description--
|
||||
|
||||
回想一下,使用加号`+`查找一个或多个字符,使用星号`*`查找零个或多个字符。这些都很方便,但有时需要匹配一定范围的匹配模式。
|
||||
|
||||
可以使用`数量说明符`指定匹配模式的上下限。数量说明符与花括号(`{`和`}`)一起使用。可以在花括号之间放两个数字,这两个数字代表匹配模式的上限和下限。
|
||||
|
||||
例如,要在字符串`"ah"`中匹配仅出现`3`到`5`次的字母`a`,正则表达式应为`/a{3,5}h/`。
|
||||
|
||||
```js
|
||||
let A4 = "aaaah";
|
||||
@ -19,57 +21,53 @@ multipleA.test(A4); // Returns true
|
||||
multipleA.test(A2); // Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>ohRegex</code>以匹配在<code>"Oh no"</code>中仅出现<code>3</code>到<code>6</code>次的字母<code>h</code>。
|
||||
</section>
|
||||
修改正则表达式`ohRegex`以匹配在`"Oh no"`中仅出现`3`到`6`次的字母`h`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用花括号。
|
||||
testString: assert(ohRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: "你的正则表达式不应该匹配<code>'Ohh no'</code>。"
|
||||
testString: assert(!ohRegex.test("Ohh no"));
|
||||
- text: "你的正则表达式应该匹配<code>'Ohhh no'</code>。"
|
||||
testString: assert("Ohhh no".match(ohRegex)[0].length === 7);
|
||||
- text: 正则表达式应该匹配 <code>"Ohhhh no"</code>。
|
||||
testString: assert("Ohhhh no".match(ohRegex)[0].length === 8);
|
||||
- text: "你的正则表达式应该匹配<code>'Ohhhhh no'</code>。"
|
||||
testString: assert("Ohhhhh no".match(ohRegex)[0].length === 9);
|
||||
- text: "你的正则表达式应该匹配<code>'Ohhhhhh no'</code>。"
|
||||
testString: assert("Ohhhhhh no".match(ohRegex)[0].length === 10);
|
||||
- text: "你的正则表达式不应该匹配<code>'Ohhhhhhh no'</code>。"
|
||||
testString: assert(!ohRegex.test("Ohhhhhhh no"));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
你的正则表达式应该使用花括号。
|
||||
|
||||
```js
|
||||
let ohStr = "Ohhh no";
|
||||
let ohRegex = /change/; // Change this line
|
||||
let result = ohRegex.test(ohStr);
|
||||
assert(ohRegex.source.match(/{.*?}/).length > 0);
|
||||
```
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式不应该匹配`'Ohh no'`。
|
||||
|
||||
```js
|
||||
let ohStr = "Ohhh no";
|
||||
let ohRegex = /Oh{3,6} no/; // Change this line
|
||||
let result = ohRegex.test(ohStr);
|
||||
assert(!ohRegex.test('Ohh no'));
|
||||
```
|
||||
|
||||
</section>
|
||||
你的正则表达式应该匹配`'Ohhh no'`。
|
||||
|
||||
```js
|
||||
assert('Ohhh no'.match(ohRegex)[0].length === 7);
|
||||
```
|
||||
|
||||
正则表达式应该匹配 `"Ohhhh no"`。
|
||||
|
||||
```js
|
||||
assert('Ohhhh no'.match(ohRegex)[0].length === 8);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Ohhhhh no'`。
|
||||
|
||||
```js
|
||||
assert('Ohhhhh no'.match(ohRegex)[0].length === 9);
|
||||
```
|
||||
|
||||
你的正则表达式应该匹配`'Ohhhhhh no'`。
|
||||
|
||||
```js
|
||||
assert('Ohhhhhh no'.match(ohRegex)[0].length === 10);
|
||||
```
|
||||
|
||||
你的正则表达式不应该匹配`'Ohhhhhhh no'`。
|
||||
|
||||
```js
|
||||
assert(!ohRegex.test('Ohhhhhhh no'));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
id: 587d7dbb367417b2b2512bab
|
||||
title: 使用捕获组搜索和替换
|
||||
challengeType: 1
|
||||
forumTopicId: 301368
|
||||
title: 使用捕获组搜索和替换
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
搜索功能是很有用的。但是,当搜索同时也执行更改(或替换)匹配文本的操作时,搜索功能就会显得更加强大。
|
||||
可以使用字符串上<code>.replace()</code>方法来搜索并替换字符串中的文本。<code>.replace()</code>的输入首先是想要搜索的正则表达式匹配模式,第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。
|
||||
|
||||
可以使用字符串上`.replace()`方法来搜索并替换字符串中的文本。`.replace()`的输入首先是想要搜索的正则表达式匹配模式,第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。
|
||||
|
||||
```js
|
||||
let wrongText = "The sky is silver.";
|
||||
@ -17,62 +18,38 @@ wrongText.replace(silverRegex, "blue");
|
||||
// Returns "The sky is blue."
|
||||
```
|
||||
|
||||
你还可以使用美元符号(<code>$</code>)访问替换字符串中的捕获组。
|
||||
你还可以使用美元符号(`$`)访问替换字符串中的捕获组。
|
||||
|
||||
```js
|
||||
"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
|
||||
// Returns "Camp Code"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
编写一个正则表达式,以搜索字符串<code>"good"</code>。然后更新变量<code>replaceText</code>,用字符串<code>"okey-dokey"</code>替换<code>"good"</code>。
|
||||
</section>
|
||||
编写一个正则表达式,以搜索字符串`"good"`。然后更新变量`replaceText`,用字符串`"okey-dokey"`替换`"good"`。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>.replace()</code>搜索并替换。
|
||||
testString: assert(code.match(/\.replace\(.*\)/));
|
||||
- text: "你的正则表达式应该把<code>'This sandwich is good.'</code>变成<code>'This sandwich is okey-dokey.'</code>。"
|
||||
testString: assert(result == "This sandwich is okey-dokey." && replaceText === "okey-dokey");
|
||||
- text: 你不应该改变最后一行。
|
||||
testString: assert(code.match(/result\s*=\s*huhText\.replace\(.*?\)/));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该使用`.replace()`搜索并替换。
|
||||
|
||||
```js
|
||||
let huhText = "This sandwich is good.";
|
||||
let fixRegex = /change/; // Change this line
|
||||
let replaceText = ""; // Change this line
|
||||
let result = huhText.replace(fixRegex, replaceText);
|
||||
assert(code.match(/\.replace\(.*\)/));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的正则表达式应该把`'This sandwich is good.'`变成`'This sandwich is okey-dokey.'`。
|
||||
|
||||
```js
|
||||
let huhText = "This sandwich is good.";
|
||||
let fixRegex = /good/g; // Change this line
|
||||
let replaceText = "okey-dokey"; // Change this line
|
||||
let result = huhText.replace(fixRegex, replaceText);
|
||||
assert(
|
||||
result == 'This sandwich is okey-dokey.' && replaceText === 'okey-dokey'
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
你不应该改变最后一行。
|
||||
|
||||
```js
|
||||
assert(code.match(/result\s*=\s*huhText\.replace\(.*?\)/));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
|
@ -1,15 +1,17 @@
|
||||
---
|
||||
id: 587d7db3367417b2b2512b8e
|
||||
title: 使用测试方法
|
||||
challengeType: 1
|
||||
forumTopicId: 301369
|
||||
title: 使用测试方法
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
在编程语言中,正则表达式用于匹配指定的字符串。通过正则表达式创建匹配模式(规则)可以帮你完成指定匹配。
|
||||
如果想要在字符串<code>"The dog chased the cat"</code>中匹配到<code>"the"</code>这个单词,可以使用如下正则表达式:<code>/the/</code>。注意,正则表达式中不需要引号。
|
||||
JavaScript 中有多种使用正则表达式的方法。测试正则表达式的一种方法是使用<code>.test()</code>方法。<code>.test()</code>方法会把编写的正则表达式和字符串(即括号内的内容)匹配,如果成功匹配到字符,则返回<code>true</code>,反之,返回<code>false</code>。
|
||||
|
||||
如果想要在字符串`"The dog chased the cat"`中匹配到`"the"`这个单词,可以使用如下正则表达式:`/the/`。注意,正则表达式中不需要引号。
|
||||
|
||||
JavaScript 中有多种使用正则表达式的方法。测试正则表达式的一种方法是使用`.test()`方法。`.test()`方法会把编写的正则表达式和字符串(即括号内的内容)匹配,如果成功匹配到字符,则返回`true`,反之,返回`false`。
|
||||
|
||||
```js
|
||||
let testStr = "freeCodeCamp";
|
||||
@ -18,51 +20,23 @@ testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
使用<code>.test()</code>方法,检测字符串<code>myString</code>是否符合正则表达式<code>myRegex</code>定义的规则。
|
||||
</section>
|
||||
使用`.test()`方法,检测字符串`myString`是否符合正则表达式`myRegex`定义的规则。
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>.test()</code>方法来检测正则表达式。
|
||||
testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
- text: 你的返回结果应该为<code>true</code>。
|
||||
testString: assert(result === true);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
你应该使用`.test()`方法来检测正则表达式。
|
||||
|
||||
```js
|
||||
let myString = "Hello, World!";
|
||||
let myRegex = /Hello/;
|
||||
let result = myRegex; // Change this line
|
||||
assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
你的返回结果应该为`true`。
|
||||
|
||||
```js
|
||||
let myString = "Hello, World!";
|
||||
let myRegex = /Hello/;
|
||||
let result = myRegex.test(myString); // Change this line
|
||||
assert(result === true);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --solutions--
|
||||
|
||||
|
Reference in New Issue
Block a user