fix(i18n): update Chinese translation of regular expressions (#38042)
Co-authored-by: Zhicheng Chen <chenzhicheng@dayuwuxian.com>
This commit is contained in:
@ -2,28 +2,43 @@
|
||||
id: 587d7dba367417b2b2512ba8
|
||||
title: Check for All or None
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301338
|
||||
localeTitle: 检查全部或无
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时,您要搜索的模式可能包含可能存在或可能不存在的模式。但是,尽管如此,检查它们可能很重要。您可以指定可能存在带问号的元素, <code>?</code> 。这将检查前一个元素中的零个或一个。您可以将此符号视为前一个元素是可选的。例如,美式英语和英式英语略有不同,您可以使用问号来匹配两种拼写。 <blockquote>让美国人=“颜色”; <br>让british =“color”; <br>让rainbowRegex = / colou?r /; <br> rainbowRegex.test(美国); //返回true <br> rainbowRegex.test(英国); //返回true </blockquote></section>
|
||||
<section id='description'>
|
||||
有时,想要搜寻的匹配模式可能有不确定是否存在的部分。尽管如此,还是想检查它们。
|
||||
为此,可以使用问号<code>?</code>指定可能存在的元素。这将检查前面的零个或一个元素。可以将此符号视为前面的元素是可选的。
|
||||
例如,美式英语和英式英语略有不同,可以使用问号来匹配两种拼写。
|
||||
|
||||
```js
|
||||
let american = "color";
|
||||
let british = "colour";
|
||||
let rainbowRegex= /colou?r/;
|
||||
rainbowRegex.test(american); // Returns true
|
||||
rainbowRegex.test(british); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改正则表达式<code>favRegex</code>以匹配该单词的美国英语(收藏)和英国英语(收藏)版本。 </section>
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>favRegex</code>以匹配美式英语(favorite)和英式英语(favourite)的单词版本。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用可选的符号, <code>?</code> 。
|
||||
- text: 你的正则表达式应该使用可选符号<code>?</code>。
|
||||
testString: assert(favRegex.source.match(/\?/).length > 0);
|
||||
- text: 你的正则表达式应该匹配<code>"favorite"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'favorite'</code>。"
|
||||
testString: assert(favRegex.test("favorite"));
|
||||
- text: 你的正则表达式应该匹配<code>"favourite"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'favourite'</code>。"
|
||||
testString: assert(favRegex.test("favourite"));
|
||||
- text: 你的正则表达式不应该匹配<code>"fav"</code>
|
||||
- text: "你的正则表达式不应该匹配<code>'fav'</code>。"
|
||||
testString: assert(!favRegex.test("fav"));
|
||||
|
||||
```
|
||||
@ -39,7 +54,6 @@ tests:
|
||||
let favWord = "favorite";
|
||||
let favRegex = /change/; // Change this line
|
||||
let result = favRegex.test(favWord);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -52,6 +66,9 @@ let result = favRegex.test(favWord);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let favWord = "favorite";
|
||||
let favRegex = /favou?r/;
|
||||
let result = favRegex.test(favWord);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 5c3dda8b4d8df89bea71600f
|
||||
title: Check For Mixed Grouping of Characters
|
||||
challengeType: 1
|
||||
forumTopicId: 301339
|
||||
localeTitle: 检查混合字符组
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
有时候我们想使用正则表达式里的括号 <code>()</code> 来检查字符组。
|
||||
如果想在字符串找到 <code>Penguin</code> 或 <code>Pumpkin</code>,可以这个正则表达式:<code>/P(engu|umpk)in/g</code>。
|
||||
然后使用 <code>test()</code> 方法检查 test 字符串里面是否包含字符组。
|
||||
|
||||
```js
|
||||
let testStr = "Pumpkin";
|
||||
let testRegex = /P(engu|umpk)in/g;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
完善正则表达式,使其以区分大小写的方式检查 <code>Franklin Roosevelt</code> 或 <code>Eleanor Roosevelt</code> 的名字,并且应该忽略 middle names。
|
||||
然后完善代码,使创建的正则检查 <code>myString</code>,根据正则是否匹配返回 <code>true</code> 或 <code>false</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```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);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
let myString = "Eleanor Roosevelt";
|
||||
let myRegex = /(Franklin|Eleanor).*Roosevelt/;
|
||||
let result = myRegex.test(myString);
|
||||
```
|
||||
|
||||
</section>
|
@ -2,26 +2,41 @@
|
||||
id: 587d7db4367417b2b2512b92
|
||||
title: Extract Matches
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 提取匹配
|
||||
forumTopicId: 301340
|
||||
localeTitle: 提取匹配项
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">到目前为止,您只是检查字符串中是否存在模式。您还可以使用<code>.match()</code>方法提取您找到的实际匹配项。要使用<code>.match()</code>方法,请将该方法应用于字符串并传入括号内的正则表达式。这是一个例子: <blockquote> “你好,世界!”。匹配(/ Hello /); <br> //返回[“Hello”] <br>让ourStr =“正则表达式”; <br>让ourRegex = / expressions /; <br> ourStr.match(ourRegex); <br> //返回[“表达式”] </blockquote></section>
|
||||
<section id='description'>
|
||||
到目前为止,只是检查了一个匹配模式是否存在于字符串中。还可以使用<code>.match()</code>方法来提取找到的实际匹配项。
|
||||
可以使用字符串来调用<code>.match()</code>方法,并在括号内传入正则表达式。以下是一个示例:
|
||||
|
||||
```js
|
||||
"Hello, World!".match(/Hello/);
|
||||
// Returns ["Hello"]
|
||||
let ourStr = "Regular expressions";
|
||||
let ourRegex = /expressions/;
|
||||
ourStr.match(ourRegex);
|
||||
// Returns ["expressions"]
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">应用<code>.match()</code>方法来提取单词<code>coding</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
利用<code>.match()</code>方法提取单词<code>coding</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>result</code>应该有单词<code>coding</code>
|
||||
- text: <code>结果</code>应该包含单词<code>coding</code>。
|
||||
testString: assert(result.join() === "coding");
|
||||
- text: 你的regex <code>codingRegex</code>应该搜索<code>coding</code>
|
||||
- text: 你的正则表达式<code>codingRegex</code>应该搜寻<code>coding</code>。
|
||||
testString: assert(codingRegex.source === "coding");
|
||||
- text: 您应该使用<code>.match()</code>方法。
|
||||
- text: 你应该使用<code>.match()</code>方法。
|
||||
testString: assert(code.match(/\.match\(.*\)/));
|
||||
|
||||
```
|
||||
@ -37,7 +52,6 @@ tests:
|
||||
let extractStr = "Extract the word 'coding' from this string.";
|
||||
let codingRegex = /change/; // Change this line
|
||||
let result = extractStr; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -50,6 +64,9 @@ let result = extractStr; // Change this line
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let extractStr = "Extract the word 'coding' from this string.";
|
||||
let codingRegex = /coding/; // Change this line
|
||||
let result = extractStr.match(codingRegex); // Change this line
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,23 +2,31 @@
|
||||
id: 587d7db6367417b2b2512b9b
|
||||
title: Find Characters with Lazy Matching
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 查找具有延迟匹配的字符
|
||||
forumTopicId: 301341
|
||||
localeTitle: 用惰性匹配来查找字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在正则表达式中, <code>greedy</code>匹配找到符合正则表达式模式的字符串的最长部分,并将其作为匹配返回。替代方案称为<code>lazy</code>匹配,它找到满足正则表达式模式的字符串的最小可能部分。您可以将regex <code>/t[az]*i/</code>应用于字符串<code>"titanic"</code> 。这个正则表达式基本上是一个以<code>t</code>开头的模式,以<code>i</code>结尾,并且在它们之间有一些字母。默认情况下,正则表达式是<code>greedy</code> ,因此匹配将返回<code>["titani"]</code> 。它找到可能适合模式的最大子串。但是,你可以使用<code>?</code>字符将其更改为<code>lazy</code>匹配。 <code>"titanic"</code>与调整后的正则表达式匹配<code>/t[az]*?i/</code> return <code>["ti"]</code> 。 </section>
|
||||
<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>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">修复regex <code>/<.*>/</code>以返回HTML标记<code><h1></code>而不是文本<code>"<h1>Winter is coming</h1>"</code> 。记住通配符<code>.</code>在正则表达式中匹配任何字符。 </section>
|
||||
<section id='instructions'>
|
||||
修复正则表达式<code>/<.*>/</code>,让它返回 HTML 标签<code><h1></code>,而不是文本<code>"<h1>Winter is coming</h1>"</code>。请记得在正则表达式中使用通配符<code>.</code>来匹配任意字符。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>result</code>变量应该是一个包含<code><h1></code>的数组
|
||||
testString: 'assert(result[0] == "<h1>", "The <code>result</code> variable should be an array with <code><h1></code> in it");'
|
||||
- text: <code>结果</code>变量应该是一个包含<code><h1></code>的数组。
|
||||
testString: assert(result[0] == '<h1>');
|
||||
|
||||
```
|
||||
|
||||
@ -33,7 +41,6 @@ tests:
|
||||
let text = "<h1>Winter is coming</h1>";
|
||||
let myRegex = /<.*>/; // Change this line
|
||||
let result = text.match(myRegex);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -46,6 +53,9 @@ let result = text.match(myRegex);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let text = "<h1>Winter is coming</h1>";
|
||||
let myRegex = /<.*?>/; // Change this line
|
||||
let result = text.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,28 +2,49 @@
|
||||
id: 587d7db4367417b2b2512b93
|
||||
title: Find More Than the First Match
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 找到比第一场比赛更多的东西
|
||||
forumTopicId: 301342
|
||||
localeTitle: 全局匹配
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">到目前为止,您只能提取或搜索一次模式。 <blockquote>让testStr =“重复,重复,重复”; <br>让ourRegex = /重复/; <br> testStr.match(ourRegex); <br> //返回[“重复”] </blockquote>要多次搜索或提取模式,可以使用<code>g</code>标志。 <blockquote> let repeatRegex = / Repeat / g; <br> testStr.match(repeatRegex); <br> //返回[“重复”,“重复”,“重复”] </blockquote></section>
|
||||
<section id='description'>
|
||||
到目前为止,只能提取或搜寻一次模式匹配。
|
||||
|
||||
```js
|
||||
let testStr = "Repeat, Repeat, Repeat";
|
||||
let ourRegex = /Repeat/;
|
||||
testStr.match(ourRegex);
|
||||
// Returns ["Repeat"]
|
||||
```
|
||||
|
||||
若要多次搜寻或提取模式匹配,可以使用<code>g</code>标志。
|
||||
|
||||
```js
|
||||
let repeatRegex = /Repeat/g;
|
||||
testStr.match(repeatRegex);
|
||||
// Returns ["Repeat", "Repeat", "Repeat"]
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用正则表达式<code>starRegex</code> ,找到并提取字符串<code>twinkleStar</code> <code>"Twinkle"</code>单词。 <strong>注意</strong> <br>你可以在你的正则表达式上有多个标志,比如<code>/search/gi</code> </section>
|
||||
<section id='instructions'>
|
||||
使用正则表达式<code>starRegex</code>,从字符串<code>twinkleStar</code>中匹配到所有的<code>"Twinkle"</code>单词并提取出来。
|
||||
<strong>注意:</strong><br>在正则表达式上可以有多个标志,比如<code>/search/gi</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>starRegex</code>应该使用全局标志<code>g</code>
|
||||
- text: 你的正则表达式<code>starRegex</code>应该使用全局标志<code>g</code>。
|
||||
testString: assert(starRegex.flags.match(/g/).length == 1);
|
||||
- text: 你的正则表达式<code>starRegex</code>应该使用不区分大小写的标志<code>i</code>
|
||||
- text: 你的正则表达式<code>starRegex</code>应该使用忽略大小写标志<code>i</code>。
|
||||
testString: assert(starRegex.flags.match(/i/).length == 1);
|
||||
- text: 您的匹配应匹配<code>"Twinkle"</code>一词的出现次数
|
||||
- text: "你的匹配应该匹配单词<code>'Twinkle'</code>的两个匹配项。"
|
||||
testString: assert(result.sort().join() == twinkleStar.match(/twinkle/gi).sort().join());
|
||||
- text: 您的匹配<code>result</code>应该包含两个元素。
|
||||
- text: 你的匹配<code>结果</code>应该包含两个元素。
|
||||
testString: assert(result.length == 2);
|
||||
|
||||
```
|
||||
@ -39,7 +60,6 @@ tests:
|
||||
let twinkleStar = "Twinkle, twinkle, little star";
|
||||
let starRegex = /change/; // Change this line
|
||||
let result = twinkleStar; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -52,6 +72,9 @@ let result = twinkleStar; // Change this line
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let twinkleStar = "Twinkle, twinkle, little star";
|
||||
let starRegex = /twinkle/gi;
|
||||
let result = twinkleStar.match(starRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,34 +2,57 @@
|
||||
id: 587d7db7367417b2b2512b9c
|
||||
title: Find One or More Criminals in a Hunt
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301343
|
||||
localeTitle: 在狩猎中找到一个或多个罪犯
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">是时候暂停和测试你的新正则表达式写作技巧了。一群罪犯逃出监狱逃跑,但你不知道有多少人。但是,你知道他们和其他人在一起时会保持紧密联系。你有责任立刻找到所有的罪犯。下面是一个查看如何执行此操作的示例:regex <code>/z+/</code>在连续出现一次或多次时匹配字母<code>z</code> 。它会在以下所有字符串中找到匹配项: <blockquote> “Z” <br> “ZZZZZZ” <br> “ABCzzzz” <br> “zzzzABC” <br> “abczzzzzzzzzzzzzzzzzzzzzabc” </blockquote>但它没有在以下字符串中找到匹配项,因为没有字母<code>z</code>字符: <blockquote> “” <br> “ABC” <br> “ABCABC” </blockquote></section>
|
||||
<section id='description'>
|
||||
是时候暂停和测试你的新正则表达式写作技巧了。一群罪犯逃出监狱逃跑,但你不知道有多少人。但是,你知道他们和其他人在一起时会保持紧密联系。你有责任立刻找到所有的罪犯。
|
||||
这里有一个示例来回顾如何做到这一点:
|
||||
当字母<code>z</code>在一行中出现一次或连续多次时,正则表达式<code>/z+/</code>会匹配到它。它会在以下所有字符串中找到匹配项:
|
||||
|
||||
```js
|
||||
"z"
|
||||
"zzzzzz"
|
||||
"ABCzzzz"
|
||||
"zzzzABC"
|
||||
"abczzzzzzzzzzzzzzzzzzzzzabc"
|
||||
```
|
||||
|
||||
但是它不会在以下字符串中找到匹配项,因为它们中没有字母<code>z</code>:
|
||||
|
||||
```js
|
||||
""
|
||||
"ABC"
|
||||
"abcabc"
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">写一个<code>greedy</code>正则表达式,在一群其他人中找到一个或多个罪犯。罪犯由大写字母<code>C</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
编写一个<code>贪婪</code>正则表达式,在一组其他人中匹配到一个或多个罪犯。罪犯由大写字母<code>C</code>表示。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您正则表达式应该匹配<code>one</code>犯罪(“ <code>C</code>中”), <code>"C"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'C'</code>中的 <em>一个</em> 罪犯('<code>C</code>')。"
|
||||
testString: assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C');
|
||||
- text: 您正则表达式应该匹配<code>two</code>罪犯(“ <code>CC</code>中”) <code>"CC"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'CC'</code>中的 <em>两个</em> 罪犯('<code>CC</code>')。"
|
||||
testString: assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC');
|
||||
- text: 你的正则表达式应匹配<code>"P1P5P4CCCP2P6P3"</code>中的<code>three</code>罪犯(“ <code>CCC</code> ”)
|
||||
- text: "你的正则表达式应该匹配<code>'P1P5P4CCCP2P6P3'</code>中的 <em>三个</em> 罪犯('<code>CCC</code>')。"
|
||||
testString: assert('P1P5P4CCCP2P6P3'.match(reCriminals) && 'P1P5P4CCCP2P6P3'.match(reCriminals)[0] == 'CCC');
|
||||
- text: 你的正则表达式应匹配<code>"P6P2P7P4P5CCCCCP3P1"</code>中的<code>five</code>罪犯(“ <code>CCCCC</code> ”)
|
||||
- text: "你的正则表达式应该匹配<code>'P6P2P7P4P5CCCCCP3P1'</code>中的 <em>五个</em> 罪犯('<code>CCCCC</code>')。"
|
||||
testString: assert('P6P2P7P4P5CCCCCP3P1'.match(reCriminals) && 'P6P2P7P4P5CCCCCP3P1'.match(reCriminals)[0] == 'CCCCC');
|
||||
- text: 你的正则表达式不应该匹配<code>""</code>中的任何罪犯
|
||||
- text: "你的正则表达式在<code>''</code>中不应该匹配到任何罪犯。"
|
||||
testString: assert(!reCriminals.test(''));
|
||||
- text: 你的正则表达式不应该匹配<code>"P1P2P3"</code>中的任何罪犯
|
||||
- text: "你的正则表达式在<code>'P1P2P3'</code>中不应该匹配到任何罪犯。"
|
||||
testString: assert(!reCriminals.test('P1P2P3'));
|
||||
- text: 您正则表达式应该与<code>fifty</code>的罪犯(“ <code>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC</code>中”) <code>"P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3"</code> 。
|
||||
- text: "你的正则表达式应该匹配<code>'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'</code>中的 <em>五十个</em> 罪犯('<code>CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC</code>')。"
|
||||
testString: assert('P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(reCriminals) && 'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'.match(reCriminals)[0] == "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC");
|
||||
|
||||
```
|
||||
@ -49,7 +72,6 @@ let reCriminals = /./; // Change this line
|
||||
|
||||
let matchedCriminals = crowd.match(reCriminals);
|
||||
console.log(matchedCriminals);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -62,6 +84,13 @@ console.log(matchedCriminals);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
// example crowd gathering
|
||||
let crowd = 'P1P2P3P4P5P6CCCP7P8P9';
|
||||
|
||||
let reCriminals = /C+/; // Change this line
|
||||
|
||||
let matchedCriminals = crowd.match(reCriminals);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,40 +2,46 @@
|
||||
id: 587d7db4367417b2b2512b91
|
||||
title: Ignore Case While Matching
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301344
|
||||
localeTitle: 匹配时忽略大小写
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">到目前为止,你已经看过正则表达式来进行字符串的字面匹配。但有时,您可能还希望匹配案例差异。大小写(或有时是大写字母大小写)是大写字母和小写字母之间的区别。大写的示例是<code>"A"</code> , <code>"B"</code>和<code>"C"</code> 。小写的示例是<code>"a"</code> , <code>"b"</code>和<code>"c"</code> 。您可以使用所谓的标志来匹配这两种情况。还有其他标志,但在这里你将专注于忽略大小写的标志 - <code>i</code>旗帜。您可以通过将其附加到正则表达式来使用它。使用此标志的示例是<code>/ignorecase/i</code> 。此正则表达式可以匹配字符串<code>"ignorecase"</code> , <code>"igNoreCase"</code>和<code>"IgnoreCase"</code> 。 </section>
|
||||
<section id='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>
|
||||
<section id='instructions'>
|
||||
编写正则表达式<code>fccRegex</code>以匹配<code>"freeCodeCamp"</code>,忽略大小写。正则表达式不应与任何缩写或带有空格的变体匹配。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该与<code>freeCodeCamp</code>匹配
|
||||
- text: 你的正则表达式应该匹配<code>freeCodeCamp</code>。
|
||||
testString: assert(fccRegex.test('freeCodeCamp'));
|
||||
- text: 你的正则表达式应该与<code>FreeCodeCamp</code>匹配
|
||||
- text: 你的正则表达式应该匹配<code>FreeCodeCamp</code>。
|
||||
testString: assert(fccRegex.test('FreeCodeCamp'));
|
||||
- text: 你的正则表达式应该与<code>FreecodeCamp</code>匹配
|
||||
- text: 你的正则表达式应该匹配<code>FreecodeCamp</code>。
|
||||
testString: assert(fccRegex.test('FreecodeCamp'));
|
||||
- text: 你的正则表达式应该与<code>FreeCodecamp</code>匹配
|
||||
- text: 你的正则表达式应该匹配<code>FreeCodecamp</code>。
|
||||
testString: assert(fccRegex.test('FreeCodecamp'));
|
||||
- text: 你的正则表达式不应该与<code>Free Code Camp</code>不匹配
|
||||
- text: 你的正则表达式不应该匹配<code>Free Code Camp</code>。
|
||||
testString: assert(!fccRegex.test('Free Code Camp'));
|
||||
- text: 你的正则表达式应该与<code>FreeCOdeCamp</code>匹配
|
||||
- text: Your regex should match<code>FreeCOdeCamp</code>。
|
||||
testString: assert(fccRegex.test('FreeCOdeCamp'));
|
||||
- text: 你的正则表达式不应该与<code>FCC</code>匹配
|
||||
- text: 你的正则表达式不应该匹配<code>FCC</code>。
|
||||
testString: assert(!fccRegex.test('FCC'));
|
||||
- text: 你的正则表达式应该与<code>FrEeCoDeCamp</code>匹配
|
||||
- text: 你的正则表达式应该匹配<code>FrEeCoDeCamp</code>。
|
||||
testString: assert(fccRegex.test('FrEeCoDeCamp'));
|
||||
- text: 你的正则表达式应该与<code>FrEeCodECamp</code>匹配
|
||||
- text: 你的正则表达式应该匹配<code>FrEeCodECamp</code>。
|
||||
testString: assert(fccRegex.test('FrEeCodECamp'));
|
||||
- text: 你的正则表达式应该与<code>FReeCodeCAmp</code>匹配
|
||||
- text: 你的正则表达式应该匹配<code>FReeCodeCAmp</code>。
|
||||
testString: assert(fccRegex.test('FReeCodeCAmp'));
|
||||
|
||||
```
|
||||
@ -51,7 +57,6 @@ tests:
|
||||
let myString = "freeCodeCamp";
|
||||
let fccRegex = /change/; // Change this line
|
||||
let result = fccRegex.test(myString);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -64,6 +69,9 @@ let result = fccRegex.test(myString);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let myString = "freeCodeCamp";
|
||||
let fccRegex = /freecodecamp/i; // Change this line
|
||||
let result = fccRegex.test(myString);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,34 +2,41 @@
|
||||
id: 587d7db4367417b2b2512b90
|
||||
title: Match a Literal String with Different Possibilities
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配具有不同可能性的文字字符串
|
||||
forumTopicId: 301345
|
||||
localeTitle: 同时用多种模式匹配文字字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">使用<code>/coding/</code>等正则表达式,可以在另一个字符串中查找<code>"coding"</code>模式。这对搜索单个字符串很有用,但它仅限于一种模式。您可以使用<code>alternation</code>或<code>OR</code>运算符搜索多个模式: <code>|</code> 。此运算符在其之前或之后匹配模式。例如,如果你想匹配<code>"yes"</code>或<code>"no"</code> ,你想要的正则表达式是<code>/yes|no/</code> 。您还可以搜索两种以上的模式。您可以通过添加更多模式来实现此操作,其中更多<code>OR</code>运算符将它们分开,例如<code>/yes|no|maybe/</code> 。 </section>
|
||||
<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>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">完成正则表达式<code>petRegex</code>以匹配宠物<code>"dog"</code> , <code>"cat"</code> , <code>"bird"</code>或<code>"fish"</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
完成正则表达式<code>petRegex</code>以匹配<code>"dog"</code>、<code>"cat"</code>、<code>"bird"</code>或者<code>"fish"</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>petRegex</code>应该为字符串<code>"John has a pet dog."</code>返回<code>true</code> <code>"John has a pet dog."</code>
|
||||
- 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>petRegex</code>应该为字符串<code>"Emma has a pet rock."</code>返回<code>false</code> <code>"Emma has a pet rock."</code>
|
||||
- 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>petRegex</code>应该为字符串<code>"Emma has a pet bird."</code>返回<code>true</code> <code>"Emma has a pet bird."</code>
|
||||
- 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>petRegex</code>应该返回<code>true</code>为字符串<code>"Liz has a pet cat."</code>
|
||||
- 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>petRegex</code>应该返回<code>false</code>为<code>"Kara has a pet dolphin."</code>的字符串<code>"Kara has a pet dolphin."</code>
|
||||
- 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>petRegex</code>应该返回<code>true</code>为字符串<code>"Alice has a pet fish."</code>
|
||||
- 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>petRegex</code>应该返回<code>false</code>为字符串<code>"Jimmy has a pet computer."</code>
|
||||
- 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.'));
|
||||
|
||||
```
|
||||
@ -45,7 +52,6 @@ tests:
|
||||
let petString = "James has a pet cat.";
|
||||
let petRegex = /change/; // Change this line
|
||||
let result = petRegex.test(petString);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -58,6 +64,9 @@ let result = petRegex.test(petString);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let petString = "James has a pet cat.";
|
||||
let petRegex = /dog|cat|bird|fish/; // Change this line
|
||||
let result = petRegex.test(petString);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,32 +2,49 @@
|
||||
id: 587d7db7367417b2b2512b9f
|
||||
title: Match All Letters and Numbers
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配所有字母和数字
|
||||
forumTopicId: 301346
|
||||
localeTitle: 匹配所有的字母和数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">使用字符类,您可以使用<code>[az]</code>搜索字母表中的所有字母。这种字符类很常见,它有一个快捷方式,虽然它还包含一些额外的字符。 JavaScript中与字母表匹配的最接近的字符类是<code>\w</code> 。此快捷方式等于<code>[A-Za-z0-9_]</code> 。此字符类匹配大写和小写字母加数字。注意,此字符类还包括下划线字符( <code>_</code> )。 <blockquote>让longHand = / [A-Za-z0-9 _] + /; <br>让shortHand = / \ w + /; <br>让数字=“42”; <br> let varNames =“important_var”; <br> longHand.test(数字); //返回true <br> shortHand.test(数字); //返回true <br> longHand.test(varNames); //返回true <br> shortHand.test(varNames); //返回true </blockquote>这些快捷方式字符类也称为<code>shorthand character classes</code> 。 </section>
|
||||
<section id='description'>
|
||||
使用元字符,可以使用<code>[a-z]</code>搜寻字母表中的所有字母。这种元字符是很常见的,它有一个缩写,但这个缩写也包含额外的字符。
|
||||
JavaScript 中与字母表匹配的最接近的元字符是<code>\w</code>,这个缩写等同于<code>[A-Za-z0-9_]</code>。它不仅可以匹配大小写字母和数字,注意,它还会匹配下划线字符(<code>_</code>)。
|
||||
|
||||
```js
|
||||
let longHand = /[A-Za-z0-9_]+/;
|
||||
let shortHand = /\w+/;
|
||||
let numbers = "42";
|
||||
let varNames = "important_var";
|
||||
longHand.test(numbers); // Returns true
|
||||
shortHand.test(numbers); // Returns true
|
||||
longHand.test(varNames); // Returns true
|
||||
shortHand.test(varNames); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用速记字符类<code>\w</code>来计算各种引号和字符串中的字母数字字符数。 </section>
|
||||
<section id='instructions'>
|
||||
使用缩写<code>\w</code>来计算所有引号中字母和数字字符的数量。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用全局标志。
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(alphabetRegexV2.global);
|
||||
- text: 你的正则表达式应该使用速记字符
|
||||
- text: 正则表达式应该使用元字符 <code>\w</code> 匹配字母表里的所有字符。
|
||||
testString: assert(/\\w/.test(alphabetRegexV2.source));
|
||||
- text: 你的正则表达式应该在<code>"The five boxing wizards jump quickly."</code>找到31个字母数字字符<code>"The five boxing wizards jump quickly."</code>
|
||||
- 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个字母数字字符<code>"Pack my box with five dozen liquor jugs."</code>
|
||||
- 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个字母数字字符<code>"How vexingly quick daft zebras jump!"</code>
|
||||
- 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个字母数字字符<code>"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."</code>
|
||||
- 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);
|
||||
|
||||
```
|
||||
@ -43,7 +60,6 @@ tests:
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let alphabetRegexV2 = /change/; // Change this line
|
||||
let result = quoteSample.match(alphabetRegexV2).length;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -56,6 +72,9 @@ let result = quoteSample.match(alphabetRegexV2).length;
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let alphabetRegexV2 = /\w/g; // Change this line
|
||||
let result = quoteSample.match(alphabetRegexV2).length;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,36 +2,41 @@
|
||||
id: 587d7db8367417b2b2512ba1
|
||||
title: Match All Non-Numbers
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301347
|
||||
localeTitle: 匹配所有非数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">最后一项挑战显示了如何使用带有小写<code>d</code>的快捷键<code>\d</code>来搜索数字。您也可以使用类似的使用大写<code>D</code>快捷方式搜索非数字。查找非数字字符的快捷方式是<code>\D</code>这等于字符类<code>[^0-9]</code> ,它查找不是0到9之间的数字的单个字符。 </section>
|
||||
<section id='description'>
|
||||
上一项挑战中展示了如何使用带有小写<code>d</code>的缩写<code>\d</code>来搜寻数字。也可以使用类似的缩写来搜寻非数字,该缩写使用大写的<code>D</code>。
|
||||
查找非数字字符的缩写是<code>\D</code>。这等同于字符串<code>[^0-9]</code>,它查找不是 0 - 9 之间数字的单个字符。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用非数字<code>\D</code>的速记字符类来计算电影标题中有多少个非数字。 </section>
|
||||
<section id='instructions'>
|
||||
使用非数字缩写<code>\D</code>来计算电影标题中有多少非数字。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的正则表达式应使用快捷方式字符来匹配非数字字符
|
||||
- text: 你的正则表达式应该使用缩写来匹配非数字字符。
|
||||
testString: assert(/\\D/.test(noNumRegex.source));
|
||||
- text: 你的正则表达式应该使用全局标志。
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(noNumRegex.global);
|
||||
- text: 你的正则表达式应该在<code>"9"</code>找不到非数字。
|
||||
- text: "你的正则表达式在<code>'9'</code>中应该匹配不到非数字。"
|
||||
testString: assert("9".match(noNumRegex) == null);
|
||||
- text: 你的正则表达式应该在<code>"Catch 22"</code>找到6个非数字。
|
||||
- text: "你的正则表达式应该在<code>'Catch 22'</code>中匹配到 6 个非数字。"
|
||||
testString: assert("Catch 22".match(noNumRegex).length == 6);
|
||||
- text: 你的正则表达式应该在<code>"101 Dalmatians"</code>找到11个非数字。
|
||||
- text: "你的正则表达式应该在<code>'101 Dalmatians'</code>中匹配到 11 个非数字。"
|
||||
testString: assert("101 Dalmatians".match(noNumRegex).length == 11);
|
||||
- text: '你的正则表达式应该在<code>"One, Two, Three"</code>找到15个非数字。'
|
||||
- text: "你的正则表达式应该在<code>'One, Two, Three'</code>中匹配到 15 个非数字。"
|
||||
testString: assert("One, Two, Three".match(noNumRegex).length == 15);
|
||||
- text: 你的正则表达式应该在<code>"21 Jump Street"</code>找到12个非数字。
|
||||
- text: "你的正则表达式应该在<code>'21 Jump Street'</code>中匹配到 12 个非数字。"
|
||||
testString: assert("21 Jump Street".match(noNumRegex).length == 12);
|
||||
- text: '你的正则表达式应该在<code>"2001: A Space Odyssey"</code>找到17个非数字。'
|
||||
- text: '你的正则表达式应该在<code>"2001: A Space Odyssey"</code>中匹配到 17 个非数字。'
|
||||
testString: 'assert("2001: A Space Odyssey".match(noNumRegex).length == 17);'
|
||||
|
||||
```
|
||||
@ -44,10 +49,9 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let numString = "Your sandwich will be $5.00";
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let noNumRegex = /change/; // Change this line
|
||||
let result = numString.match(noNumRegex).length;
|
||||
|
||||
let result = movieName.match(noNumRegex).length;
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -60,6 +64,9 @@ let result = numString.match(noNumRegex).length;
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let noNumRegex = /\D/g; // Change this line
|
||||
let result = movieName.match(noNumRegex).length;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,36 +2,41 @@
|
||||
id: 5d712346c441eddfaeb5bdef
|
||||
title: Match All Numbers
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配所有号码
|
||||
forumTopicId: 18181
|
||||
localeTitle: 匹配所有数字
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您已经学习了常用字符串模式(如字母数字)的快捷方式。另一种常见模式是寻找数字或数字。查找数字字符的快捷方式是<code>\d</code> ,小写<code>d</code> 。这等于字符类<code>[0-9]</code> ,它查找0到9之间任意数字的单个字符。 </section>
|
||||
<section id='description'>
|
||||
已经了解了常见字符串匹配模式的元字符,如字母数字。另一个常见的匹配模式是只寻找数字。
|
||||
查找数字字符的缩写是<code>\d</code>,注意是小写的<code>d</code>。这等同于元字符<code>[0-9]</code>,它查找 0 到 9 之间任意数字的单个字符。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用速记字符类<code>\d</code>来计算电影标题中的位数。写出的数字(“六”而不是六)不计算在内。 </section>
|
||||
<section id='instructions'>
|
||||
使用缩写<code>\d</code>来计算电影标题中有多少个数字。书面数字("six" 而不是 6)不计算在内。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您的正则表达式应使用快捷方式字符来匹配数字字符
|
||||
- text: 你的正则表达式应该使用缩写来匹配数字字符。
|
||||
testString: assert(/\\d/.test(numRegex.source));
|
||||
- text: 你的正则表达式应该使用全局标志。
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(numRegex.global);
|
||||
- text: 你的正则表达式应该在<code>"9"</code>找到1位数。
|
||||
- text: "你的正则表达式应该在<code>'9'</code>中匹配到 1 个数字。"
|
||||
testString: assert("9".match(numRegex).length == 1);
|
||||
- text: 你的正则表达式应该在<code>"Catch 22"</code>找到2位数字。
|
||||
- text: "你的正则表达式应该在<code>'Catch 22'</code>中匹配到 2 个数字。"
|
||||
testString: assert("Catch 22".match(numRegex).length == 2);
|
||||
- text: 你的正则表达式应该在<code>"101 Dalmatians"</code>找到3位数字。
|
||||
- text: "你的正则表达式应该在<code>'101 Dalmatians'</code>中匹配到 3 个数字。"
|
||||
testString: assert("101 Dalmatians".match(numRegex).length == 3);
|
||||
- text: '你的正则表达式应该在<code>"One, Two, Three"</code>找不到数字。'
|
||||
- text: "你的正则表达式在<code>'One, Two, Three'</code>中应该匹配不到数字。"
|
||||
testString: assert("One, Two, Three".match(numRegex) == null);
|
||||
- text: 您的正则表达式应该在<code>"21 Jump Street"</code>找到2位数字。
|
||||
- text: "你的正则表达式应该在<code>'21 Jump Street'</code>中匹配到 2 个数字。"
|
||||
testString: assert("21 Jump Street".match(numRegex).length == 2);
|
||||
- text: '你的正则表达式应该在<code>"2001: A Space Odyssey"</code>找到4位数字。'
|
||||
- text: '你的正则表达式应该在<code>"2001: A Space Odyssey"</code>中匹配到 4 个数字。'
|
||||
testString: 'assert("2001: A Space Odyssey".match(numRegex).length == 4);'
|
||||
|
||||
```
|
||||
@ -44,10 +49,9 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let numString = "Your sandwich will be $5.00";
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let numRegex = /change/; // Change this line
|
||||
let result = numString.match(numRegex).length;
|
||||
|
||||
let result = movieName.match(numRegex).length;
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -60,6 +64,10 @@ let result = numString.match(numRegex).length;
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let movieName = "2001: A Space Odyssey";
|
||||
let numRegex = /\d/g; // Change this line
|
||||
let result = movieName.match(numRegex).length;
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,40 +2,54 @@
|
||||
id: 587d7db5367417b2b2512b94
|
||||
title: Match Anything with Wildcard Period
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配通配符期间的任何内容
|
||||
forumTopicId: 301348
|
||||
localeTitle: 用通配符.匹配任何内容
|
||||
---
|
||||
|
||||
## 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>来匹配所有四个单词。 <blockquote>让humStr =“我会哼唱一首歌”; <br>让hugStr =“熊抱”; <br>让huRegex = /hu./; <br> humStr.match(huRegex); //返回[“hum”] <br> hugStr.match(huRegex); //返回[“拥抱”] </blockquote></section>
|
||||
<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>匹配以上四个单词。
|
||||
|
||||
```js
|
||||
let humStr = "I'll hum a song";
|
||||
let hugStr = "Bear hug";
|
||||
let huRegex = /hu./;
|
||||
huRegex.test(humStr); // Returns true
|
||||
huRegex.test(hugStr); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## 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>
|
||||
<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>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该使用<code>.test()</code>方法。
|
||||
- text: 你应该使用<code>.test()</code>方法。
|
||||
testString: assert(code.match(/\.test\(.*\)/));
|
||||
- text: 您应该在正则表达式<code>unRegex</code>使用通配符
|
||||
- text: 你应该在你的正则表达式<code>unRegex</code>中使用通配符。
|
||||
testString: assert(/\./.test(unRegex.source));
|
||||
- text: 你的正则表达式<code>unRegex</code>应该匹配<code>"run"</code> <code>"Let us go on a run."</code> <code>"run"</code>中的<code>"run"</code> <code>"Let us go on a run."</code>
|
||||
- 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>"sun"</code> <code>"The sun is out today."</code> <code>"sun"</code>中的<code>"sun"</code>匹配<code>"The sun is out today."</code>
|
||||
- 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>"fun"</code> <code>"Coding is a lot of fun."</code> <code>"fun"</code>中的<code>"fun"</code>匹配<code>"Coding is a lot of fun."</code>
|
||||
- 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>"pun"</code> <code>"Seven days without a pun makes one weak."</code> <code>"pun"</code> <code>"Seven days without a pun makes one weak."</code>
|
||||
- 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>"nun"</code> <code>"One takes a vow to be a nun."</code> <code>"nun"</code>中的<code>"nun"</code>匹配<code>"One takes a vow to be a nun."</code>
|
||||
- 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>"bun"</code> <code>"She got fired from the hot dog stand for putting her hair in a bun."</code> <code>"bun"</code>中的<code>"bun"</code> <code>"She got fired from the hot dog stand for putting her hair in a bun."</code>
|
||||
- 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>匹配<code>"There is a bug in my code."</code>
|
||||
- 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>
|
||||
- text: "你的正则表达式<code>unRegex</code>不应该匹配<code>'Catch me if you can.'</code>。"
|
||||
testString: assert(!unRegex.test("Can me if you can."));
|
||||
|
||||
```
|
||||
@ -51,7 +65,6 @@ tests:
|
||||
let exampleStr = "Let's have fun with regular expressions!";
|
||||
let unRegex = /change/; // Change this line
|
||||
let result = unRegex.test(exampleStr);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -64,6 +77,9 @@ let result = unRegex.test(exampleStr);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let exampleStr = "Let's have fun with regular expressions!";
|
||||
let unRegex = /.un/; // Change this line
|
||||
let result = unRegex.test(exampleStr);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,28 +2,44 @@
|
||||
id: 587d7db7367417b2b2512b9d
|
||||
title: Match Beginning String Patterns
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配开始字符串模式
|
||||
forumTopicId: 301349
|
||||
localeTitle: 匹配字符串的开头
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">先前的挑战表明,正则表达式可用于寻找许多匹配。它们还用于搜索字符串中特定位置的模式。在之前的挑战中,您使用<code>character set</code>内的<code>caret</code>符( <code>^</code> )来创建<code>[^thingsThatWillNotBeMatched]</code>形式的<code>negated character set</code> 。在<code>character set</code> , <code>caret</code>用于在字符串的开头搜索模式。 <blockquote>让firstString =“Ricky是第一个,可以找到。”; <br>让firstRegex = / ^ Ricky /; <br> firstRegex.test(firstString); <br> //返回true <br>让notFirst =“你现在找不到Ricky了。”; <br> firstRegex.test(notFirst); <br> //返回false </blockquote></section>
|
||||
<section id='description'>
|
||||
回顾一下之前的挑战,正则表达式可以用于查找多项匹配。还可以查询字符串中符合指定匹配模式的字符。
|
||||
在之前的挑战中,使用<code>字符集</code>中的<code>插入</code>符号(<code>^</code>)来创建一个<code>否定字符集</code>,形如<code>[^thingsThatWillNotBeMatched]</code>。在<code>字符集</code>之外,<code>插入</code>符号用于字符串的开头搜寻匹配模式。
|
||||
|
||||
```js
|
||||
let firstString = "Ricky is first and can be found.";
|
||||
let firstRegex = /^Ricky/;
|
||||
firstRegex.test(firstString);
|
||||
// Returns true
|
||||
let notFirst = "You can't find Ricky now.";
|
||||
firstRegex.test(notFirst);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用正则表达式中的<code>caret</code>只能在字符串<code>rickyAndCal</code>的开头找到<code>"Cal"</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
在正则表达式中使用<code>^</code>符号,以匹配仅在字符串<code>rickyAndCal</code>的开头出现的<code>"Cal"</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该用大写字母搜索<code>"Cal"</code> 。
|
||||
- text: "你的正则表达式应该搜寻有一个大写字母的<code>'Cal'</code>。"
|
||||
testString: assert(calRegex.source == "^Cal");
|
||||
- text: 你的正则表达式不应该使用任何标志。
|
||||
testString: assert(calRegex.flags == "");
|
||||
- text: 你的正则表达式应该匹配字符串开头的<code>"Cal"</code> 。
|
||||
- text: "你的正则表达式应该匹配字符串开头的<code>'Cal'</code>。"
|
||||
testString: assert(calRegex.test("Cal and Ricky both like racing."));
|
||||
- text: 您的正则表达式不应与字符串中间的<code>"Cal"</code>匹配。
|
||||
- text: "你的正则表达式不应该匹配字符串中间的<code>'Cal'</code>。"
|
||||
testString: assert(!calRegex.test("Ricky and Cal both like racing."));
|
||||
|
||||
```
|
||||
@ -39,7 +55,6 @@ tests:
|
||||
let rickyAndCal = "Cal and Ricky both like racing.";
|
||||
let calRegex = /change/; // Change this line
|
||||
let result = calRegex.test(rickyAndCal);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -52,6 +67,9 @@ let result = calRegex.test(rickyAndCal);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let rickyAndCal = "Cal and Ricky both like racing.";
|
||||
let calRegex = /^Cal/; // Change this line
|
||||
let result = calRegex.test(rickyAndCal);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,15 +2,22 @@
|
||||
id: 587d7db6367417b2b2512b99
|
||||
title: Match Characters that Occur One or More Times
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301350
|
||||
localeTitle: 匹配出现一次或多次的字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="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> <code>"bcd"</code> ,因此找不到匹配项。 </section>
|
||||
<section id='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>
|
||||
<section id='instructions'>
|
||||
想要在字符串<code>"Mississippi"</code>中匹配到出现一次或多次的字母<code>s</code>的匹配项。编写一个使用<code>+</code>符号的正则表达式。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
@ -19,9 +26,9 @@ localeTitle: 匹配出现一次或多次的字符
|
||||
tests:
|
||||
- text: 你的正则表达式<code>myRegex</code>应该使用<code>+</code>符号来匹配一个或多个<code>s</code>字符。
|
||||
testString: assert(/\+/.test(myRegex.source));
|
||||
- text: 你的正则表达式<code>myRegex</code>应该匹配2个项目。
|
||||
- text: 你的正则表达式<code>myRegex</code>应该匹配两项。
|
||||
testString: assert(result.length == 2);
|
||||
- text: <code>result</code>变量应该是一个包含两个匹配<code>"ss"</code>的数组
|
||||
- text: "<code>结果</code>变量应该是一个包含两个<code>'ss'</code>匹配项的数组。"
|
||||
testString: assert(result[0] == 'ss' && result[1] == 'ss');
|
||||
|
||||
```
|
||||
@ -37,7 +44,6 @@ tests:
|
||||
let difficultSpelling = "Mississippi";
|
||||
let myRegex = /change/; // Change this line
|
||||
let result = difficultSpelling.match(myRegex);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -50,6 +56,9 @@ let result = difficultSpelling.match(myRegex);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let difficultSpelling = "Mississippi";
|
||||
let myRegex = /s+/g; // Change this line
|
||||
let result = difficultSpelling.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,31 +2,49 @@
|
||||
id: 587d7db6367417b2b2512b9a
|
||||
title: Match Characters that Occur Zero or More Times
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301351
|
||||
localeTitle: 匹配出现零次或多次的字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">最后一项挑战使用加<code>+</code>号来查找出现一次或多次的字符。还有一个选项可以匹配出现零次或多次的字符。执行此操作的字符是<code>asterisk</code>或<code>star</code> <code>asterisk</code> : <code>*</code> 。 <blockquote>让soccerWord =“gooooooooal!”; <br>让gPhrase =“直觉”; <br>让oPhrase =“越过月亮”; <br> let goRegex = / go * /; <br> soccerWord.match(goRegex); //返回[“goooooooo”] <br> gPhrase.match(goRegex); //返回[“g”] <br> oPhrase.match(goRegex); //返回null </blockquote></section>
|
||||
<section id='description'>
|
||||
上一次的挑战中使用了加号<code>+</code>来查找出现一次或多次的字符。还有一个选项可以匹配出现零次或多次的字符。
|
||||
执行该操作的字符叫做<code>asterisk</code>或<code>star</code>,即<code>*</code>。
|
||||
|
||||
```js
|
||||
let soccerWord = "gooooooooal!";
|
||||
let gPhrase = "gut feeling";
|
||||
let oPhrase = "over the moon";
|
||||
let goRegex = /go*/;
|
||||
soccerWord.match(goRegex); // Returns ["goooooooo"]
|
||||
gPhrase.match(goRegex); // Returns ["g"]
|
||||
oPhrase.match(goRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个正则表达式<code>chewieRegex</code>使用的<code>*</code>字符匹配所有上下<code>"a"</code>中的字符<code>chewieQuote</code> 。你的正则表达式不需要标志,它不应该与任何其他引号相匹配。 </section>
|
||||
<section id='instructions'>
|
||||
在这个挑战里,<code>chewieQuote</code> 已经被初始化为 "Aaaaaaaaaaaaaaaarrrgh!"。创建一个变量为<code>chewieRegex</code>的正则表达式,使用<code>*</code>符号在<code>chewieQuote</code>中匹配<code>"A"</code>及其之后出现的零个或多个<code>"a"</code>。你的正则表达式不需要使用修饰符,也不需要匹配引号。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您正则表达式<code>chewieRegex</code>应该使用<code>*</code>字符匹配零个或多个<code>a</code>字符。
|
||||
- text: "你的正则表达式<code>chewieRegex</code>应该使用<code>*</code>符号匹配<code>'A'</code>之后出现的零个或多个<code>'a'</code>字符。"
|
||||
testString: assert(/\*/.test(chewieRegex.source));
|
||||
- text: 你的正则表达式<code>chewieRegex</code>应匹配16个字符。
|
||||
- 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>"Aaaaaaaaaaaaaaaa"</code> 。
|
||||
testString: assert(result[0] === "Aaaaaaaaaaaaaaaa");
|
||||
- text: '你的正则表达式不应该与<code>"He made a fair move. Screaming about it can't help you."</code>中的任何角色相匹配<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>中的任何角色相匹配<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));
|
||||
- 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));
|
||||
|
||||
```
|
||||
|
||||
@ -38,15 +56,20 @@ tests:
|
||||
<div id='js-seed'>
|
||||
|
||||
```js
|
||||
let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
|
||||
let chewieRegex = /change/; // Change this line
|
||||
let chewieRegex = /change/; // Only change this line
|
||||
let result = chewieQuote.match(chewieRegex);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## Before Test
|
||||
<div id='js-setup'>
|
||||
|
||||
```js
|
||||
const chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
@ -54,6 +77,8 @@ let result = chewieQuote.match(chewieRegex);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let chewieRegex = /Aa*/;
|
||||
let result = chewieQuote.match(chewieRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,26 +2,43 @@
|
||||
id: 587d7db7367417b2b2512b9e
|
||||
title: Match Ending String Patterns
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配结束字符串模式
|
||||
forumTopicId: 301352
|
||||
localeTitle: 匹配字符串的末尾
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在上一个挑战中,您学会了使用<code>caret</code>来搜索字符串开头的模式。还有一种方法可以在字符串末尾搜索模式。您可以使用正则表达式末尾的<code>dollar sign</code>字符<code>$</code>来搜索字符串的结尾。 <blockquote>让theEnding =“这是一个永无止境的故事”; <br>让storyRegex = / story $ /; <br> storyRegex.test(theEnding); <br> //返回true <br>让noEnding =“有时故事必须结束”; <br> storyRegex.test(noEnding); <br> //返回false <br></blockquote></section>
|
||||
<section id='description'>
|
||||
在上一个挑战中,学习了使用<code>^</code>符号来搜寻字符串开头的匹配模式。还有一种方法可以搜寻字符串末尾的匹配模式。
|
||||
可以使用正则表达式的<code>美元</code>符号<code>$</code>来搜寻字符串的结尾。
|
||||
|
||||
```js
|
||||
let theEnding = "This is a never ending story";
|
||||
let storyRegex = /story$/;
|
||||
storyRegex.test(theEnding);
|
||||
// Returns true
|
||||
let noEnding = "Sometimes a story will have to end";
|
||||
storyRegex.test(noEnding);
|
||||
// Returns false
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用锚字符( <code>$</code> )来匹配字符串<code>"caboose"</code>在字符串的结尾<code>caboose</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
使用<code>$</code>在字符串<code>caboose</code>的末尾匹配<code>"caboose"</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该在正则表达式中使用美元符号<code>$</code> anchor搜索<code>"caboose"</code> 。
|
||||
- text: "你应该在正则表达式使用美元符号<code>$</code>来搜寻<code>'caboose'</code>。"
|
||||
testString: assert(lastRegex.source == "caboose$");
|
||||
- text: 你的正则表达式不应该使用任何标志。
|
||||
testString: assert(lastRegex.flags == "");
|
||||
- text: 您应该在字符串末尾匹配<code>"caboose"</code> <code>"The last car on a train is the caboose"</code>
|
||||
- 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"));
|
||||
|
||||
```
|
||||
@ -37,7 +54,6 @@ tests:
|
||||
let caboose = "The last car on a train is the caboose";
|
||||
let lastRegex = /change/; // Change this line
|
||||
let result = lastRegex.test(caboose);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -50,6 +66,9 @@ let result = lastRegex.test(caboose);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let caboose = "The last car on a train is the caboose";
|
||||
let lastRegex = /caboose$/; // Change this line
|
||||
let result = lastRegex.test(caboose);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,32 +2,46 @@
|
||||
id: 587d7db8367417b2b2512ba0
|
||||
title: Match Everything But Letters and Numbers
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配一切,但字母和数字
|
||||
forumTopicId: 301353
|
||||
localeTitle: 匹配除了字母和数字的所有符号
|
||||
---
|
||||
|
||||
## 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> 。 <blockquote>让shortHand = / \ W /; <br>让数字=“42%”; <br> let sentence =“Coding!”; <br> numbers.match(简写); //返回[“%”] <br> sentence.match(简写); //返回[“!”] <br></blockquote></section>
|
||||
<section id='description'>
|
||||
已经了解到可以使用缩写<code>\w</code>来匹配字母和数字<code>[A-Za-z0-9_]</code>。不过,有可能想要搜寻的匹配模式是非字母数字字符。
|
||||
可以使用<code>\W</code>搜寻和<code>\w</code>相反的匹配模式。注意,相反匹配模式使用大写字母。此缩写与<code>[^A-Za-z0-9_]</code>是一样的。
|
||||
|
||||
```js
|
||||
let shortHand = /\W/;
|
||||
let numbers = "42%";
|
||||
let sentence = "Coding!";
|
||||
numbers.match(shortHand); // Returns ["%"]
|
||||
sentence.match(shortHand); // Returns ["!"]
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用速记字符类<code>\W</code>来计算各种引号和字符串中的非字母数字字符数。 </section>
|
||||
<section id='instructions'>
|
||||
使用缩写<code>\W</code>来计算不同引号和字符串中非字母数字字符的数量。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用全局标志。
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(nonAlphabetRegex.global);
|
||||
- text: 你的正则表达式应该在<code>"The five boxing wizards jump quickly."</code>找到6个非字母数字字符<code>"The five boxing wizards jump quickly."</code> 。
|
||||
- text: "你的正则表达式应该在<code>'The five boxing wizards jump quickly.'</code>中匹配到 6 个非字母数字字符。"
|
||||
testString: assert("The five boxing wizards jump quickly.".match(nonAlphabetRegex).length == 6);
|
||||
- text: 你的正则表达式应该使用速记字符。
|
||||
- text: 正则表达式应该使用元字符来匹配非字母字符。
|
||||
testString: assert(/\\W/.test(nonAlphabetRegex.source));
|
||||
- text: 你的正则表达式应该在<code>"Pack my box with five dozen liquor jugs."</code>找到8个非字母数字字符<code>"Pack my box with five dozen liquor jugs."</code>
|
||||
- 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个非字母数字字符<code>"How vexingly quick daft zebras jump!"</code>
|
||||
- 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个非字母数字字符<code>"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."</code>
|
||||
- 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);
|
||||
|
||||
```
|
||||
@ -43,7 +57,6 @@ tests:
|
||||
let quoteSample = "The five boxing wizards jump quickly.";
|
||||
let nonAlphabetRegex = /change/; // Change this line
|
||||
let result = quoteSample.match(nonAlphabetRegex).length;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -56,6 +69,9 @@ let result = quoteSample.match(nonAlphabetRegex).length;
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let quoteSample = "The five boxing wizards_jump quickly.";
|
||||
let nonAlphabetRegex = /\W/g; // Change this line
|
||||
let result = quoteSample.match(nonAlphabetRegex).length;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,26 +2,44 @@
|
||||
id: 587d7db5367417b2b2512b96
|
||||
title: Match Letters of the Alphabet
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配字母的字母
|
||||
forumTopicId: 301354
|
||||
localeTitle: 匹配字母表中的字母
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您了解了如何使用<code>character sets</code>来指定要匹配的一组字符,但是当您需要匹配大范围的字符(例如,字母表中的每个字母)时,这是很多类型。幸运的是,有一个内置功能,使这简短。在<code>character set</code> ,您可以使用<code>hyphen</code>字符来定义要匹配的<code>hyphen</code>范围: <code>-</code> 。例如,要匹配小写字母<code>a</code>到<code>e</code>您将使用<code>[ae]</code> 。 <blockquote>让catStr =“猫”; <br>让batStr =“蝙蝠”; <br>让matStr =“mat”; <br>让bgRegex = / [ae] at /; <br> catStr.match(bgRegex); //返回[“cat”] <br> batStr.match(bgRegex); //返回[“bat”] <br> matStr.match(bgRegex); //返回null </blockquote></section>
|
||||
<section id='description'>
|
||||
了解了如何使用<code>字符集</code>来指定要匹配的一组字符串,但是当需要匹配大量字符(例如,字母表中的每个字母)时,有一种写法可以让实现这个功能变得简短。
|
||||
在<code>字符集</code>中,可以使用<code>连字符</code>(<code>-</code>)来定义要匹配的字符范围。
|
||||
例如,要匹配小写字母<code>a</code>到<code>e</code>,你可以使用<code>[a-e]</code>。
|
||||
|
||||
```js
|
||||
let catStr = "cat";
|
||||
let batStr = "bat";
|
||||
let matStr = "mat";
|
||||
let bgRegex = /[a-e]at/;
|
||||
catStr.match(bgRegex); // Returns ["cat"]
|
||||
batStr.match(bgRegex); // Returns ["bat"]
|
||||
matStr.match(bgRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">匹配字符串<code>quoteSample</code>中的所有字母。 <strong>注意</strong> <br>务必匹配大写和小写<strong>字母<strong>。</strong></strong> </section>
|
||||
<section id='instructions'>
|
||||
匹配字符串<code>quoteSample</code>中的所有字母。
|
||||
<strong>注意:</strong><br>一定要同时匹配大小写<strong>字母<strong>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>alphabetRegex</code>应该匹配35项。
|
||||
- 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>应该使用不区分大小写的标志。
|
||||
- text: 你的正则表达式<code>alphabetRegex</code>应该使用忽略大小写标志。
|
||||
testString: assert(alphabetRegex.flags.match(/i/).length == 1);
|
||||
|
||||
```
|
||||
@ -37,7 +55,6 @@ tests:
|
||||
let quoteSample = "The quick brown fox jumps over the lazy dog.";
|
||||
let alphabetRegex = /change/; // Change this line
|
||||
let result = alphabetRegex; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -50,6 +67,9 @@ let result = alphabetRegex; // Change this line
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
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
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,26 +2,47 @@
|
||||
id: 587d7db3367417b2b2512b8f
|
||||
title: Match Literal Strings
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301355
|
||||
localeTitle: 匹配文字字符串
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">在上一次挑战中,您使用正则表达式<code>/Hello/</code>搜索了单词<code>"Hello"</code> 。该正则表达式搜索字符串<code>"Hello"</code>的文字匹配。这是另一个搜索字符串<code>"Kevin"</code>的文字匹配的示例: <blockquote>让testStr =“你好,我的名字是凯文。”; <br>让testRegex = / Kevin /; <br> testRegex.test(testStr); <br> //返回true </blockquote>任何其他形式的<code>"Kevin"</code>都不匹配。例如,正则表达式<code>/Kevin/</code>将不匹配<code>"kevin"</code>或<code>"KEVIN"</code> 。 <blockquote> let wrongRegex = / kevin /; <br> wrongRegex.test(testStr); <br> //返回false </blockquote>未来的挑战将展示如何匹配其他形式。 </section>
|
||||
<section id='description'>
|
||||
在上一个挑战中,使用正则表达式<code>/Hello/</code>搜索到了字符串<code>"Hello"</code>。那个正则表达式在字符串中搜寻<code>"Hello"</code>的文字匹配。下面是另一个在字符串中搜寻<code>"Kevin"</code>的示例:
|
||||
|
||||
```js
|
||||
let testStr = "Hello, my name is Kevin.";
|
||||
let testRegex = /Kevin/;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
任何其他形式的<code>"Kevin"</code>都不会被匹配。例如,正则表达式<code>/Kevin/</code>不会匹配<code>"kevin"</code>或者<code>"KEVIN"</code>。
|
||||
|
||||
```js
|
||||
let wrongRegex = /kevin/;
|
||||
wrongRegex.test(testStr);
|
||||
// Returns false
|
||||
```
|
||||
|
||||
后续的挑战将为你展示如何匹配其他形式的字符串。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">完成正则表达式<code>waldoRegex</code>在字符串<code>waldoIsHiding</code>使用文字匹配查找<code>"Waldo"</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
完成正则表达式<code>waldoRegex</code>,在字符串<code>waldoIsHiding</code>中匹配到文本<code>"Waldo"</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>waldoRegex</code>应该找到<code>"Waldo"</code>
|
||||
- text: "你的正则表达式<code>waldoRegex</code>应该匹配到<code>'Waldo'</code>。"
|
||||
testString: assert(waldoRegex.test(waldoIsHiding));
|
||||
- text: 你的正则表达式<code>waldoRegex</code>不应该搜索任何其他内容。
|
||||
- text: 你的正则表达式<code>waldoRegex</code>不应该搜寻其他的任何内容。
|
||||
testString: assert(!waldoRegex.test('Somewhere is hiding in this text.'));
|
||||
- text: 您应该与正则表达式执行文字字符串匹配。
|
||||
- text: 你应该使用你的正则表达式对字符串执行文字匹配。
|
||||
testString: assert(!/\/.*\/i/.test(code));
|
||||
|
||||
```
|
||||
@ -37,7 +58,6 @@ tests:
|
||||
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
|
||||
let waldoRegex = /search/; // Change this line
|
||||
let result = waldoRegex.test(waldoIsHiding);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -50,6 +70,9 @@ let result = waldoRegex.test(waldoIsHiding);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
|
||||
let waldoRegex = /Waldo/; // Change this line
|
||||
let result = waldoRegex.test(waldoIsHiding);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,30 +2,42 @@
|
||||
id: 587d7db9367417b2b2512ba4
|
||||
title: Match Non-Whitespace Characters
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 18210
|
||||
localeTitle: 匹配非空白字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您学会了使用<code>\s</code>搜索空格,并使用小写<code>s</code> 。您还可以搜索除空格之外的所有内容。使用<code>\S</code>搜索非空格, <code>\S</code>是一个大写的<code>s</code> 。此模式将不匹配空格,回车符,制表符,换页符和换行符。你可以认为它类似于字符类<code>[^ \r\t\f\n\v]</code> 。 <blockquote>让whiteSpace =“空白。到处都是空白!” <br>让nonSpaceRegex = / \ S / g; <br> whiteSpace.match(nonSpaceRegex)。长度; //返回32 </blockquote></section>
|
||||
<section id='description'>
|
||||
已经学会了如何使用带有小写<code>s</code>的缩写<code>\s</code>来搜寻空白字符。还可以搜寻除了空格之外的所有内容。
|
||||
使用<code>\S</code>搜寻非空白字符,其中<code>S</code>是大写。此匹配模式将不匹配空格、回车符、制表符、换页符和换行符。可以认为这类似于元字符<code>[^\r\t\f\n\v]</code>。
|
||||
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
let nonSpaceRegex = /\S/g;
|
||||
whiteSpace.match(nonSpaceRegex).length; // Returns 32
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改正则表达式<code>countNonWhiteSpace</code>以在字符串中查找多个非空白字符。 </section>
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>countNonWhiteSpace</code>以查找字符串中的多个非空字符。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用全局标志。
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(countNonWhiteSpace.global);
|
||||
- text: 你的正则表达式应该使用速记字符
|
||||
- text: 正则表达式应该使用元字符 <code>\S/code> 来匹配所有的非空格字符。
|
||||
testString: assert(/\\S/.test(countNonWhiteSpace.source));
|
||||
- text: 你的正则表达式应该在<code>"Men are from Mars and women are from Venus."</code>找到35个非空格<code>"Men are from Mars and women are from Venus."</code>
|
||||
- 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个非空格<code>"Space: the final frontier."</code>'
|
||||
- text: '你的正则表达式应该在<code>"Space: the final frontier."</code>中匹配到 23 个非空白字符。'
|
||||
testString: 'assert("Space: the final frontier.".match(countNonWhiteSpace).length == 23);'
|
||||
- text: 你的正则表达式应该在<code>"MindYourPersonalSpace"</code>找到21个非空格
|
||||
- text: "你的正则表达式应该在<code>'MindYourPersonalSpace'</code>中匹配到 21 个非空白字符。"
|
||||
testString: assert("MindYourPersonalSpace".match(countNonWhiteSpace).length == 21);
|
||||
|
||||
```
|
||||
@ -41,7 +53,6 @@ tests:
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countNonWhiteSpace = /change/; // Change this line
|
||||
let result = sample.match(countNonWhiteSpace);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -54,6 +65,9 @@ let result = sample.match(countNonWhiteSpace);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countNonWhiteSpace = /\S/g; // Change this line
|
||||
let result = sample.match(countNonWhiteSpace);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,26 +2,40 @@
|
||||
id: 587d7db5367417b2b2512b97
|
||||
title: Match Numbers and Letters of the Alphabet
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配数字和字母的字母
|
||||
forumTopicId: 301356
|
||||
localeTitle: 匹配字母表中的数字和字母
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">使用连字符( <code>-</code> )匹配一系列字符不仅限于字母。它也适用于匹配一系列数字。例如, <code>/[0-5]/</code>匹配<code>0</code>到<code>5</code>之间的任何数字,包括<code>0</code>和<code>5</code> 。此外,可以在单个字符集中组合一系列字母和数字。 <blockquote>让jennyStr =“Jenny8675309”; <br>让myRegex = / [a-z0-9] / ig; <br> //匹配jennyStr中的所有字母和数字<br> jennyStr.match(myRegex); </blockquote></section>
|
||||
<section id='description'>
|
||||
使用连字符(<code>-</code>)匹配字符范围并不仅限于字母。它还可以匹配一系列数字。
|
||||
例如,<code>/[0-5]/</code>匹配<code>0</code>和<code>5</code>之间的任意数字,包含<code>0</code>和<code>5</code>。
|
||||
此外,还可以在单个字符集中组合一系列字母和数字。
|
||||
|
||||
```js
|
||||
let jennyStr = "Jenny8675309";
|
||||
let myRegex = /[a-z0-9]/ig;
|
||||
// matches all letters and numbers in jennyStr
|
||||
jennyStr.match(myRegex);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个与<code>h</code>和<code>s</code>之间的字母范围匹配的正则表达式,以及介于<code>2</code>和<code>6</code>之间的数字范围。请记住在正则表达式中包含适当的标志。 </section>
|
||||
<section id='instructions'>
|
||||
创建一个正则表达式,使其可以匹配<code>h</code>和<code>s</code>之间的一系列字母,以及<code>2</code>和<code>6</code>之间的一系列数字。请记得在正则表达式中包含恰当的标志。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>myRegex</code>应该匹配17项。
|
||||
- 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>应该使用不区分大小写的标志。
|
||||
- text: 你的正则表达式<code>myRegex</code>应该使用忽略大小写的标志。
|
||||
testString: assert(myRegex.flags.match(/i/).length == 1);
|
||||
|
||||
```
|
||||
@ -37,7 +51,6 @@ tests:
|
||||
let quoteSample = "Blueberry 3.141592653s are delicious.";
|
||||
let myRegex = /change/; // Change this line
|
||||
let result = myRegex; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -50,6 +63,10 @@ let result = myRegex; // Change this line
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let quoteSample = "Blueberry 3.141592653s are delicious.";
|
||||
let myRegex = /[h-s2-6]/gi; // Change this line
|
||||
let result = quoteSample.match(myRegex); // Change this line
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,30 +2,50 @@
|
||||
id: 587d7db5367417b2b2512b95
|
||||
title: Match Single Character with Multiple Possibilities
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 将单个角色与多种可能性相匹配
|
||||
forumTopicId: 301357
|
||||
localeTitle: 将单个字符与多种可能性匹配
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您学习了如何匹配文字模式( <code>/literal/</code> )和通配符( <code>/./</code> )。这些是正则表达式的极端,其中一个找到完全匹配,另一个匹配一切。有两个极端之间可以平衡的选项。您可以使用<code>character classes</code>搜索具有一定灵活性的文字模式。字符类允许您通过将它们放在方括号( <code>[</code>和<code>]</code> )括号内来定义要匹配的一组字符。例如,您想匹配<code>"bag"</code> , <code>"big"</code>和<code>"bug"</code>但不匹配<code>"bog"</code> 。您可以创建regex <code>/b[aiu]g/</code>来执行此操作。 <code>[aiu]</code>是仅匹配字符<code>"a"</code> , <code>"i"</code>或<code>"u"</code>的字符类。 <blockquote>让bigStr =“大”; <br>让bagStr =“bag”; <br>让bugStr =“bug”; <br>让bogStr =“bog”; <br>让bgRegex = / b [aiu] g /; <br> bigStr.match(bgRegex); //返回[“大”] <br> bagStr.match(bgRegex); //返回[“bag”] <br> bugStr.match(bgRegex); //返回[“bug”] <br> bogStr.match(bgRegex); //返回null </blockquote></section>
|
||||
<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>的字符集。
|
||||
|
||||
```js
|
||||
let bigStr = "big";
|
||||
let bagStr = "bag";
|
||||
let bugStr = "bug";
|
||||
let bogStr = "bog";
|
||||
let bgRegex = /b[aiu]g/;
|
||||
bigStr.match(bgRegex); // Returns ["big"]
|
||||
bagStr.match(bgRegex); // Returns ["bag"]
|
||||
bugStr.match(bgRegex); // Returns ["bug"]
|
||||
bogStr.match(bgRegex); // Returns null
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在正则表达式<code>vowelRegex</code>使用带元音( <code>a</code> , <code>e</code> , <code>i</code> , <code>o</code> , <code>u</code> )的字符类来查找字符串<code>quoteSample</code>中的所有元音。 <strong>注意</strong> <br>确保匹配大写和小写元音。 </section>
|
||||
<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>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该找到所有25个元音。
|
||||
- text: 你应该匹配到所有25个元音。
|
||||
testString: assert(result.length == 25);
|
||||
- text: 你的正则表达式<code>vowelRegex</code>应该使用一个字符类。
|
||||
- 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>应该使用不区分大小写的标志。
|
||||
- text: 你的正则表达式<code>vowelRegex</code>应该使用忽略大小写标志。
|
||||
testString: assert(vowelRegex.flags.match(/i/).length == 1);
|
||||
- text: 你的正则表达式不应该与任何辅音匹配。
|
||||
- text: 你的正则表达式不应该匹配任何辅音。
|
||||
testString: assert(!/[b-df-hj-np-tv-z]/gi.test(result.join()));
|
||||
|
||||
```
|
||||
@ -41,7 +61,6 @@ tests:
|
||||
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
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -54,6 +73,9 @@ let result = vowelRegex; // Change this line
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
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
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,26 +2,32 @@
|
||||
id: 587d7db6367417b2b2512b98
|
||||
title: Match Single Characters Not Specified
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配未指定的单个字符
|
||||
forumTopicId: 301358
|
||||
localeTitle: 匹配单个未指定的字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">到目前为止,您已创建了一组要匹配的字符,但您也可以创建一组您不想匹配的字符。这些类型的字符集称为<code>negated character sets</code> 。要创建<code>negated character set</code> ,请在<code>caret</code>括号后面和不想匹配的字符前放置一个<code>caret</code> ( <code>^</code> )。例如, <code>/[^aeiou]/gi</code>匹配所有不是元音的字符。请注意字符之类的<code>.</code> , <code>!</code> , <code>[</code> , <code>@</code> , <code>/</code>和空格匹配 - 否定元音字符集仅排除元音字符。 </section>
|
||||
<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>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">创建一个匹配所有不是数字或元音的字符的正则表达式。请记住在正则表达式中包含适当的标志。 </section>
|
||||
<section id='instructions'>
|
||||
创建一个匹配所有非数字或元音字符的正则表达式。请记得在正则表达式中包含恰当的标志。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式<code>myRegex</code>应匹配9项。
|
||||
- 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>应该使用不区分大小写的标志。
|
||||
- text: 你的正则表达式<code>myRegex</code>应该使用忽略大小写标志。
|
||||
testString: assert(myRegex.flags.match(/i/).length == 1);
|
||||
|
||||
```
|
||||
@ -37,7 +43,6 @@ tests:
|
||||
let quoteSample = "3 blind mice.";
|
||||
let myRegex = /change/; // Change this line
|
||||
let result = myRegex; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -50,6 +55,9 @@ let result = myRegex; // Change this line
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let quoteSample = "3 blind mice.";
|
||||
let myRegex = /[^0-9aeiou]/gi; // Change this line
|
||||
let result = quoteSample.match(myRegex); // Change this line
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,30 +2,43 @@
|
||||
id: 587d7db8367417b2b2512ba3
|
||||
title: Match Whitespace
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 匹配空白
|
||||
forumTopicId: 301359
|
||||
localeTitle: 匹配空白字符
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">迄今为止的挑战包括匹配的字母和数字字母。您还可以匹配字母之间的空格或空格。您可以使用<code>\s</code>搜索空格,这是一个小写的<code>s</code> 。此模式不仅匹配空格,还匹配回车符,制表符,换页符和换行符。你可以认为它类似于字符类<code>[ \r\t\f\n\v]</code> 。 <blockquote>让whiteSpace =“空白。到处都是空白!” <br>让spaceRegex = / \ s / g; <br> whiteSpace.match(spaceRegex); <br> //返回[“”,“”] <br></blockquote></section>
|
||||
<section id='description'>
|
||||
迄今为止的挑战包括匹配的字母和数字。还可以匹配字母之间的空格。
|
||||
可以使用<code>\s</code>搜寻空格,其中<code>s</code>是小写。此匹配模式不仅匹配空格,还匹配回车符、制表符、换页符和换行符,可以将其视为与<code>[\r\t\f\n\v]</code>类似。
|
||||
|
||||
```js
|
||||
let whiteSpace = "Whitespace. Whitespace everywhere!"
|
||||
let spaceRegex = /\s/g;
|
||||
whiteSpace.match(spaceRegex);
|
||||
// Returns [" ", " "]
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改正则表达式<code>countWhiteSpace</code>以查找字符串中的多个空格字符。 </section>
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>countWhiteSpace</code>查找字符串中的多个空白字符。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用全局标志。
|
||||
- text: 你的正则表达式应该使用全局状态修正符。
|
||||
testString: assert(countWhiteSpace.global);
|
||||
- text: 你的正则表达式应该使用速记字符
|
||||
- text: 正则表达式应该使用元字符 <code>\s</code> 匹配所有的空白。
|
||||
testString: assert(/\\s/.test(countWhiteSpace.source));
|
||||
- text: 你的正则表达式应该在<code>"Men are from Mars and women are from Venus."</code>找到八个空格<code>"Men are from Mars and women are from Venus."</code>
|
||||
- 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>找到三个空格<code>"Space: the final frontier."</code>'
|
||||
- text: '你的正则表达式应该在<code>"Space: the final frontier."</code>中匹配到 3 个空白字符。'
|
||||
testString: 'assert("Space: the final frontier.".match(countWhiteSpace).length == 3);'
|
||||
- text: 您的正则表达式应该在<code>"MindYourPersonalSpace"</code>中找不到空格
|
||||
- text: "你的正则表达式在<code>'MindYourPersonalSpace'</code>中应该匹配不到空白字符。"
|
||||
testString: assert("MindYourPersonalSpace".match(countWhiteSpace) == null);
|
||||
|
||||
```
|
||||
@ -41,7 +54,6 @@ tests:
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countWhiteSpace = /change/; // Change this line
|
||||
let result = sample.match(countWhiteSpace);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -54,6 +66,9 @@ let result = sample.match(countWhiteSpace);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let sample = "Whitespace is important in separating words";
|
||||
let countWhiteSpace = /\s/g;
|
||||
let result = sample.match(countWhiteSpace);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,37 +2,69 @@
|
||||
id: 587d7dba367417b2b2512ba9
|
||||
title: Positive and Negative Lookahead
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 积极和消极的前瞻
|
||||
forumTopicId: 301360
|
||||
localeTitle: 正向先行断言和负向先行断言
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description"> <code>Lookaheads</code>是一种模式,它告诉JavaScript在字符串中向前看以进一步检查模式。当您想要在同一个字符串上搜索多个模式时,这非常有用。有两种<code>lookaheads</code> : <code>positive lookahead</code>和<code>negative lookahead</code> 。一个<code>positive lookahead</code>向前看将确保搜索模式中的元素存在,但实际上不匹配它。正向前瞻用作<code>(?=...)</code> ,其中<code>...</code>是不匹配的必需部分。另一方面, <code>negative lookahead</code>将确保搜索模式中的元素不存在。负向前瞻用作<code>(?!...)</code> ,其中<code>...</code>是您不希望在那里的模式。如果不存在负前瞻部分,则返回模式的其余部分。前瞻有点令人困惑,但一些例子会有所帮助。 <blockquote>让quit =“qu”; <br>让noquit =“qt”; <br>让quRegex = / q(?= u)/; <br>让qRegex = / q(?!u)/; <br> quit.match(quRegex); //返回[“q”] <br> noquit.match(qRegex); //返回[“q”] </blockquote> <code>lookaheads</code>更实际用途是检查一个字符串中的两个或更多个模式。这是一个(天真)简单的密码检查器,可以查找3到6个字符和至少一个数字: <blockquote> let password =“abc123”; <br>让checkPass = /(?= \ w {3,6})(?= \ D * \ d)/; <br> checkPass.test(密码); //返回true </blockquote></section>
|
||||
<section id='description'>
|
||||
<code>先行断言</code>是告诉 JavaScript 在字符串中向前查找的匹配模式。当想要在同一个字符串上搜寻多个匹配模式时,这可能非常有用。
|
||||
有两种<code>先行断言</code>:<code>正向先行断言</code>和<code>负向先行断言</code>。
|
||||
<code>正向先行断言</code>会查看并确保搜索匹配模式中的元素存在,但实际上并不匹配。正向先行断言的用法是<code>(?=...)</code>,其中<code>...</code>就是需要存在但不会被匹配的部分。
|
||||
另一方面,<code>负向先行断言</code>会查看并确保搜索匹配模式中的元素不存在。负向先行断言的用法是<code>(?!...)</code>,其中<code>...</code>是希望不存在的匹配模式。如果负向先行断言部分不存在,将返回匹配模式的其余部分。
|
||||
尽管先行断言有点儿令人困惑,但是这些示例会有所帮助。
|
||||
|
||||
```js
|
||||
let quit = "qu";
|
||||
let noquit = "qt";
|
||||
let quRegex= /q(?=u)/;
|
||||
let qRegex = /q(?!u)/;
|
||||
quit.match(quRegex); // Returns ["q"]
|
||||
noquit.match(qRegex); // Returns ["q"]
|
||||
```
|
||||
|
||||
<code>先行断言</code>的更实际用途是检查一个字符串中的两个或更多匹配模式。这里有一个简单的密码检查器,密码规则是 3 到 6 个字符且至少包含一个数字:
|
||||
|
||||
```js
|
||||
let password = "abc123";
|
||||
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
|
||||
checkPass.test(password); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>lookaheads</code>在<code>pwRegex</code>匹配长的时间大于5个字符,并有两个连续的数字密码。 </section>
|
||||
<section id='instructions'>
|
||||
在正则表达式<code>pwRegex</code>中使用<code>先行断言</code>以匹配大于5个字符且有两个连续数字的密码,并且不能以数字开头。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用两个积极的<code>lookaheads</code> 。
|
||||
- text: 你的正则表达式应该使用两个正向<code>先行断言</code>。
|
||||
testString: assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);
|
||||
- text: 你的正则表达式不应该匹配<code>"astronaut"</code>
|
||||
- text: "你的正则表达式不应该匹配<code>'astronaut'</code>。"
|
||||
testString: assert(!pwRegex.test("astronaut"));
|
||||
- text: 你的正则表达式不应该与<code>"airplanes"</code>匹配
|
||||
- text: "你的正则表达式不应该匹配<code>'airplanes'</code>。"
|
||||
testString: assert(!pwRegex.test("airplanes"));
|
||||
- text: 你的正则表达式不应该匹配<code>"banan1"</code>
|
||||
- text: 正则不应该匹配 <code>"banan1"</code>
|
||||
testString: assert(!pwRegex.test("banan1"));
|
||||
- text: 你的正则表达式应该匹配<code>"bana12"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'bana12'</code>。"
|
||||
testString: assert(pwRegex.test("bana12"));
|
||||
- text: 你的正则表达式应该匹配<code>"abc123"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'abc123'</code>。"
|
||||
testString: assert(pwRegex.test("abc123"));
|
||||
- text: 你的正则表达式不应该匹配<code>"123"</code>
|
||||
- text: "你的正则表达式不应该匹配<code>'123'</code>。"
|
||||
testString: assert(!pwRegex.test("123"));
|
||||
- text: 你的正则表达式不应该匹配<code>"1234"</code>
|
||||
- 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"));
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
||||
@ -47,7 +79,6 @@ tests:
|
||||
let sampleWord = "astronaut";
|
||||
let pwRegex = /change/; // Change this line
|
||||
let result = pwRegex.test(sampleWord);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -59,7 +90,9 @@ let result = pwRegex.test(sampleWord);
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
|
||||
```js
|
||||
// solution required
|
||||
var pwRegex = /^(?=\w{6})(?=\D+\d{2})/;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,26 +2,31 @@
|
||||
id: 587d7dbb367417b2b2512bac
|
||||
title: Remove Whitespace from Start and End
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 从开始和结束中删除空格
|
||||
forumTopicId: 301362
|
||||
localeTitle: 删除开头和结尾的空白
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">有时字符串周围的空白字符不是必需的,而是存在的。字符串的典型处理是删除字符串开头和结尾处的空格。 </section>
|
||||
<section id='description'>
|
||||
有时字符串周围存在的空白字符并不是必需的。字符串的典型处理是删除字符串开头和结尾处的空格。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">编写一个正则表达式并使用适当的字符串方法删除字符串开头和结尾的空格。 <strong>注意</strong> <br> <code>.trim()</code>方法可以在这里工作,但您需要使用正则表达式完成此挑战。 </section>
|
||||
<section id='instructions'>
|
||||
编写一个正则表达式并使用适当的字符串方法删除字符串开头和结尾的空格。
|
||||
<strong>注意:</strong><br><code>.trim()</code>方法在这里也可以实现同样的效果,但是你需要使用正则表达式来完成此项挑战。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>result</code>应该等于<code>"Hello, World!"</code>'
|
||||
- text: "<code>结果</code>应该等于<code>'Hello, World!'</code>。"
|
||||
testString: assert(result == "Hello, World!");
|
||||
- text: 您不应该使用<code>.trim()</code>方法。
|
||||
testString: assert(!code.match(/\.trim\([\s\S]*?\)/));
|
||||
- text: <code>result</code>变量不应设置为等于字符串。
|
||||
- text: 你不应该使用<code>.trim()</code>方法。
|
||||
testString: assert(!code.match(/\.trim\(.*?\)/));
|
||||
- text: <code>结果</code>变量不应该设置为等于字符串。
|
||||
testString: assert(!code.match(/result\s*=\s*".*?"/));
|
||||
|
||||
```
|
||||
@ -37,7 +42,6 @@ tests:
|
||||
let hello = " Hello, World! ";
|
||||
let wsRegex = /change/; // Change this line
|
||||
let result = hello; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -50,6 +54,9 @@ let result = hello; // Change this line
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let hello = " Hello, World! ";
|
||||
let wsRegex = /^(\s+)(.+[^\s])(\s+)$/;
|
||||
let result = hello.replace(wsRegex, '$2');
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,33 +2,52 @@
|
||||
id: 587d7db8367417b2b2512ba2
|
||||
title: Restrict Possible Usernames
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301363
|
||||
localeTitle: 限制可能的用户名
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">用户名在互联网上随处可见。它们是用户在自己喜欢的网站上获得独特身份的原因。您需要检查数据库中的所有用户名。以下是用户在创建用户名时必须遵循的一些简单规则。 1)用户名中的唯一数字必须在最后。最后可以有零个或多个。 2)用户名字母可以是小写和大写。 3)用户名必须至少两个字符长。双字母用户名只能使用字母字母。 </section>
|
||||
<section id='description'>
|
||||
用户名在互联网上随处可见。它们是用户在自己喜欢的网站上的唯一身份。
|
||||
需要检索数据库中的所有用户名。以下是用户在创建用户名时必须遵守的一些简单规则。
|
||||
1) 用户名只能是数字字母字符。
|
||||
2) 用户名中的数字必须在最后,且数字可以有零个或多个。
|
||||
3) 用户名字母可以是小写字母和大写字母。
|
||||
4) 用户名长度必须至少为两个字符。两位用户名只能使用字母。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改正则表达式<code>userCheck</code>以适合上面列出的约束。 </section>
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>userCheck</code>以适合上面列出的约束。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该与<code>JACK</code>匹配
|
||||
testString: 'assert(userCheck.test("JACK"), "Your regex should match <code>JACK</code>");'
|
||||
- text: 你的正则表达式不应该与<code>J</code>匹配
|
||||
testString: 'assert(!userCheck.test("J"), "Your regex should not match <code>J</code>");'
|
||||
- text: 你的正则表达式应该与<code>Oceans11</code>匹配
|
||||
testString: 'assert(userCheck.test("Oceans11"), "Your regex should match <code>Oceans11</code>");'
|
||||
- text: 你的正则表达式应该与<code>RegexGuru</code>匹配
|
||||
testString: 'assert(userCheck.test("RegexGuru"), "Your regex should match <code>RegexGuru</code>");'
|
||||
- text: 你的正则表达式不应该与<code>007</code>匹配
|
||||
testString: 'assert(!userCheck.test("007"), "Your regex should not match <code>007</code>");'
|
||||
- text: 你的正则表达式不应该匹配<code>9</code>
|
||||
testString: 'assert(!userCheck.test("9"), "Your regex should not match <code>9</code>");'
|
||||
- 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"));
|
||||
|
||||
```
|
||||
|
||||
@ -43,7 +62,6 @@ tests:
|
||||
let username = "JackOfAllTrades";
|
||||
let userCheck = /change/; // Change this line
|
||||
let result = userCheck.test(username);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -56,6 +74,9 @@ let result = userCheck.test(username);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let username = "JackOfAllTrades";
|
||||
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
|
||||
let result = userCheck.test(username);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,40 +2,56 @@
|
||||
id: 587d7dbb367417b2b2512baa
|
||||
title: Reuse Patterns Using Capture Groups
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301364
|
||||
localeTitle: 使用捕获组重用模式
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您搜索的某些模式将在字符串中多次出现。手动重复该正则表达式是浪费的。有一种更好的方法可以指定何时在字符串中有多个重复子字符串。您可以使用<code>capture groups</code>搜索重复子字符串。括号<code>(</code>和<code>)</code>用于查找重复子串。你把模式的正则表达式重复在括号之间。要指定重复字符串的显示位置,请使用反斜杠( <code>\</code> ),然后使用数字。此数字从1开始,随着您使用的每个其他捕获组而增加。一个例子是<code>\1</code>来匹配第一组。下面的示例匹配以空格分隔的两次出现的任何单词: <blockquote>让repeatStr =“正则表达式正则表达式”; <br> let repeatRegex = /(\ w +)\ s \ 1 /; <br> repeatRegex.test(repeatStr); //返回true <br> repeatStr.match(repeatRegex); //返回[“regex regex”,“regex”] </blockquote>对字符串使用<code>.match()</code>方法将返回一个数组,其中包含与其匹配的字符串及其捕获组。 </section>
|
||||
<section id='description'>
|
||||
一些你所搜寻的匹配模式会在字符串中出现多次,手动重复该正则表达式太浪费了。有一种更好的方法可以指定何时在字符串中会有多个重复的子字符串。
|
||||
可以使用<code>捕获组</code>搜寻重复的子字符串。括号<code>(</code>和<code>)</code>可以用来匹配重复的子字符串。只需要把重复匹配模式的正则表达式放在括号中即可。
|
||||
要指定重复字符串将出现的位置,可以使用反斜杠(<code>\</code>)后接一个数字。这个数字从 1 开始,随着你使用的每个捕获组的增加而增加。这里有一个示例,<code>\1</code>可以匹配第一个组。
|
||||
下面的示例匹配任意两个被空格分割的单词:
|
||||
|
||||
```js
|
||||
let repeatStr = "regex regex";
|
||||
let repeatRegex = /(\w+)\s\1/;
|
||||
repeatRegex.test(repeatStr); // Returns true
|
||||
repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"]
|
||||
```
|
||||
|
||||
在字符串上使用<code>.match()</code>方法将返回一个数组,其中包含它匹配的字符串及其捕获组。
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">在<code>reRegex</code>使用<code>capture groups</code>来匹配在字符串中仅重复三次的数字,每个数字用空格分隔。 </section>
|
||||
<section id='instructions'>
|
||||
在正则表达式<code>reRegex</code>中使用<code>捕获组</code>,以匹配在字符串中仅重复三次的数字,每一个都由空格分隔。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用数字的速记字符类。
|
||||
- text: 你的正则表达式应该使用数字的速记元字符。
|
||||
testString: assert(reRegex.source.match(/\\d/));
|
||||
- text: 您的正则表达式应该重复使用捕获组两次。
|
||||
- text: 你的正则表达式应该重用两次捕获组。
|
||||
testString: assert(reRegex.source.match(/\\1|\\2/g).length >= 2);
|
||||
- text: 你的正则表达式应该有两个空格来分隔这三个数字。
|
||||
- text: 你的正则表达式应该有两个空格分隔这三个数字。
|
||||
testString: assert(reRegex.source.match(/ |\\s/g).length === 2 || reRegex.source.match(/\(\\s\)(?=.*\\(1|2))/g));
|
||||
- text: 你的正则表达式应该匹配<code>"42 42 42"</code> 。
|
||||
- text: "你的正则表达式应该匹配<code>'42 42 42'</code>。"
|
||||
testString: assert(reRegex.test("42 42 42"));
|
||||
- text: 你的正则表达式应该匹配<code>"100 100 100"</code> 。
|
||||
- text: "你的正则表达式应该匹配<code>'100 100 100'</code>。"
|
||||
testString: assert(reRegex.test("100 100 100"));
|
||||
- text: 你的正则表达式不应该匹配<code>"42 42 42 42"</code> 。
|
||||
- text: "你的正则表达式不应该匹配<code>'42 42 42 42'</code>。"
|
||||
testString: assert.equal(("42 42 42 42").match(reRegex.source), null);
|
||||
- text: 你的正则表达式不应该匹配<code>"42 42"</code> 。
|
||||
- text: "你的正则表达式不应该匹配<code>'42 42'</code>。"
|
||||
testString: assert.equal(("42 42").match(reRegex.source), null);
|
||||
- text: 你的正则表达式不应该匹配<code>"101 102 103"</code> 。
|
||||
- text: "你的正则表达式不应该匹配<code>'101 102 103'</code>。"
|
||||
testString: assert(!reRegex.test("101 102 103"));
|
||||
- text: 你的正则表达式不应该匹配<code>"1 2 3"</code> 。
|
||||
- text: "你的正则表达式不应该匹配<code>'1 2 3'</code>。"
|
||||
testString: assert(!reRegex.test("1 2 3"));
|
||||
- text: 你的正则表达式应匹配<code>"10 10 10"</code> 。
|
||||
- text: "你的正则表达式应该匹配<code>'10 10 10'</code>。"
|
||||
testString: assert(reRegex.test("10 10 10"));
|
||||
|
||||
```
|
||||
@ -51,7 +67,6 @@ tests:
|
||||
let repeatNum = "42 42 42";
|
||||
let reRegex = /change/; // Change this line
|
||||
let result = reRegex.test(repeatNum);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -64,6 +79,9 @@ let result = reRegex.test(repeatNum);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let repeatNum = "42 42 42";
|
||||
let reRegex = /^(\d+)\s\1\s\1$/;
|
||||
let result = reRegex.test(repeatNum);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,32 +2,49 @@
|
||||
id: 587d7db9367417b2b2512ba7
|
||||
title: Specify Exact Number of Matches
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 指定完全匹配数
|
||||
forumTopicId: 301365
|
||||
localeTitle: 指定匹配的确切数量
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用大括号<code>quantity specifiers</code>的较低和较高数量的模式。有时您只需要特定数量的匹配。要指定一定数量的模式,只需在大括号之间放置一个数字。例如,要仅将单词<code>"hah"</code>与字母<code>a</code>匹配<code>3</code>次,您的正则表达式将为<code>/ha{3}h/</code> 。 <blockquote>让A4 =“haaaah”; <br>让A3 =“haaah”; <br>设A100 =“h”+“a”.repeat(100)+“h”; <br> let multipleHA = / ha {3} h /; <br> multipleHA.test(A4); //返回false <br> multipleHA.test(A3); //返回true <br> multipleHA.test(A100); //返回false </blockquote></section>
|
||||
<section id='description'>
|
||||
可以使用带有花括号的<code>数量说明符</code>来指定匹配模式的上下限。但有时只需要特定数量的匹配。
|
||||
要指定一定数量的匹配模式,只需在大括号之间放置一个数字。
|
||||
例如,要只匹配字母<code>a</code>出现<code>3</code>次的单词<code>"hah"</code>,正则表达式应为<code>/ha{3}h/</code>。
|
||||
|
||||
```js
|
||||
let A4 = "haaaah";
|
||||
let A3 = "haaah";
|
||||
let A100 = "h" + "a".repeat(100) + "h";
|
||||
let multipleHA = /ha{3}h/;
|
||||
multipleHA.test(A4); // Returns false
|
||||
multipleHA.test(A3); // Returns true
|
||||
multipleHA.test(A100); // Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">只有当它有四个字母<code>m</code>时才更改正则表达式<code>timRegex</code>以匹配单词<code>"Timber"</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>timRegex</code>,以匹配仅有四个字母单词<code>m</code>的单词<code>"Timber"</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用大括号。
|
||||
- text: 你的正则表达式应该使用花括号。
|
||||
testString: assert(timRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: 你的正则表达式不应该与<code>"Timber"</code>匹配
|
||||
- text: "你的正则表达式不应该匹配<code>'Timber'</code>。"
|
||||
testString: assert(!timRegex.test("Timber"));
|
||||
- text: 你的正则表达式不应该匹配<code>"Timmber"</code>
|
||||
- text: "你的正则表达式不应该匹配<code>'Timmber'</code>。"
|
||||
testString: assert(!timRegex.test("Timmber"));
|
||||
- text: 你的正则表达式不应该匹配<code>"Timmmber"</code>
|
||||
- text: "你的正则表达式不应该匹配<code>'Timmmber'</code>。"
|
||||
testString: assert(!timRegex.test("Timmmber"));
|
||||
- text: 你的正则表达式应该匹配<code>"Timmmmber"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'Timmmmber'</code>。"
|
||||
testString: assert(timRegex.test("Timmmmber"));
|
||||
- text: 你的正则表达式不应该与30 <code>m</code>的<code>"Timber"</code>相匹配。
|
||||
- text: "你的正则表达式不应该匹配包含 30 个字母<code>m</code>的<code>'Timber'</code>。"
|
||||
testString: assert(!timRegex.test("Ti" + "m".repeat(30) + "ber"));
|
||||
|
||||
```
|
||||
@ -43,7 +60,6 @@ tests:
|
||||
let timStr = "Timmmmber";
|
||||
let timRegex = /change/; // Change this line
|
||||
let result = timRegex.test(timStr);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -56,6 +72,9 @@ let result = timRegex.test(timStr);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let timStr = "Timmmmber";
|
||||
let timRegex = /Tim{4}ber/; // Change this line
|
||||
let result = timRegex.test(timStr);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,34 +2,51 @@
|
||||
id: 587d7db9367417b2b2512ba6
|
||||
title: Specify Only the Lower Number of Matches
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 仅指定较低的匹配数
|
||||
forumTopicId: 301366
|
||||
localeTitle: 只指定匹配的下限
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">您可以使用大括号<code>quantity specifiers</code>的较低和较高数量的模式。有时您只想指定较低数量的模式而没有上限。要仅指定较少的模式数,请保留第一个数字后跟逗号。例如,要仅匹配字符串<code>"hah"</code>与出现至少<code>3</code>次的字母<code>a</code> ,您的正则表达式将是<code>/ha{3,}h/</code> 。 <blockquote>让A4 =“haaaah”; <br>让A2 =“哈哈”; <br>设A100 =“h”+“a”.repeat(100)+“h”; <br> let multipleA = / ha {3,} h /; <br> multipleA.test(A4); //返回true <br> multipleA.test(A2); //返回false <br> multipleA.test(A100); //返回true </blockquote></section>
|
||||
<section id='description'>
|
||||
可以使用带有花括号的<code>数量说明符</code>来指定匹配模式的上下限。但有时候只想指定匹配模式的下限而不需要指定上限。
|
||||
为此,在第一个数字后面跟一个逗号即可。
|
||||
例如,要匹配至少出现<code>3</code>次字母<code>a</code>的字符串<code>"hah"</code>,正则表达式应该是<code>/ha{3,}h/</code>。
|
||||
|
||||
```js
|
||||
let A4 = "haaaah";
|
||||
let A2 = "haah";
|
||||
let A100 = "h" + "a".repeat(100) + "h";
|
||||
let multipleA = /ha{3,}h/;
|
||||
multipleA.test(A4); // Returns true
|
||||
multipleA.test(A2); // Returns false
|
||||
multipleA.test(A100); // Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">只有当它有四个或更多字母<code>z</code>时才更改正则表达式<code>haRegex</code>以匹配单词<code>"Hazzah"</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>haRegex</code>,匹配包含四个或更多字母<code>z</code>的单词<code>"Hazzah"</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用大括号。
|
||||
- text: 你的正则表达式应该使用花括号。
|
||||
testString: assert(haRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: 你的正则表达式不应该与<code>"Hazzah"</code>匹配
|
||||
- text: "你的正则表达式不应该匹配<code>'Hazzah'</code>。"
|
||||
testString: assert(!haRegex.test("Hazzah"));
|
||||
- text: 你的正则表达式不应该与<code>"Hazzzah"</code>匹配
|
||||
- text: "你的正则表达式不应该匹配<code>'Hazzzah'</code>。"
|
||||
testString: assert(!haRegex.test("Hazzzah"));
|
||||
- text: 你的正则表达应该匹配<code>"Hazzzzah"</code>
|
||||
- text: 正则表达式应该匹配 <code>"Hazzzzah"</code>
|
||||
testString: assert("Hazzzzah".match(haRegex)[0].length === 8);
|
||||
- text: 你的正则表达应该匹配<code>"Hazzzzzah"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'Hazzzzah'</code>。"
|
||||
testString: assert("Hazzzzzah".match(haRegex)[0].length === 9);
|
||||
- text: 你的正则表达应该匹配<code>"Hazzzzzzah"</code>
|
||||
- text: 正则表达式应该匹配 <code>"Hazzzzzzah"</code>
|
||||
testString: assert("Hazzzzzzah".match(haRegex)[0].length === 10);
|
||||
- text: 你的正则表达式应该匹配<code>"Hazzah"</code>和30个<code>z</code> 。
|
||||
- text: 正则表达式应该匹配 <code>"Hazzah"</code> with 30 <code>z</code>'s in it.
|
||||
testString: assert("Hazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzah".match(haRegex)[0].length === 34);
|
||||
|
||||
```
|
||||
@ -45,7 +62,6 @@ tests:
|
||||
let haStr = "Hazzzzah";
|
||||
let haRegex = /change/; // Change this line
|
||||
let result = haRegex.test(haStr);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -58,6 +74,9 @@ let result = haRegex.test(haStr);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let haStr = "Hazzzzah";
|
||||
let haRegex = /Haz{4,}ah/; // Change this line
|
||||
let result = haRegex.test(haStr);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,34 +2,49 @@
|
||||
id: 587d7db9367417b2b2512ba5
|
||||
title: Specify Upper and Lower Number of Matches
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 指定上下匹配数
|
||||
forumTopicId: 301367
|
||||
localeTitle: 指定匹配的上限和下限
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">回想一下,您使用加号<code>+</code>来查找一个或多个字符,使用星号<code>*</code>来查找零个或多个字符。这些很方便,但有时你想要匹配一定范围的模式。您可以使用<code>quantity specifiers</code>模式的下限和上限。数量说明符与大括号( <code>{</code>和<code>}</code> )一起使用。您在大括号之间放置了两个数字 - 用于较低和较高的模式数。例如,为了匹配字母<code>"ah"</code>出现<code>3</code>到<code>5</code>次的字母<code>a</code> ,你的正则表达式将是<code>/a{3,5}h/</code> 。 <blockquote>让A4 =“aaaah”; <br>让A2 =“aah”; <br>令multipleA = / a {3,5} h /; <br> multipleA.test(A4); //返回true <br> multipleA.test(A2); //返回false </blockquote></section>
|
||||
<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>。
|
||||
|
||||
```js
|
||||
let A4 = "aaaah";
|
||||
let A2 = "aah";
|
||||
let multipleA = /a{3,5}h/;
|
||||
multipleA.test(A4); // Returns true
|
||||
multipleA.test(A2); // Returns false
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">更改正则表达式<code>ohRegex</code>以匹配单词<code>"Oh no"</code>中的<code>3</code>到<code>6</code>字母<code>h</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
修改正则表达式<code>ohRegex</code>以匹配在<code>"Oh no"</code>中仅出现<code>3</code>到<code>6</code>次的字母<code>h</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你的正则表达式应该使用大括号。
|
||||
- text: 你的正则表达式应该使用花括号。
|
||||
testString: assert(ohRegex.source.match(/{.*?}/).length > 0);
|
||||
- text: 你的正则表达式不应该匹配<code>"Ohh no"</code>
|
||||
- text: "你的正则表达式不应该匹配<code>'Ohh no'</code>。"
|
||||
testString: assert(!ohRegex.test("Ohh no"));
|
||||
- text: 你的正则表达式应该匹配<code>"Ohhh no"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'Ohhh no'</code>。"
|
||||
testString: assert("Ohhh no".match(ohRegex)[0].length === 7);
|
||||
- text: 你的正则表达式应该匹配<code>"Ohhhh no"</code>
|
||||
- text: 正则表达式应该匹配 <code>"Ohhhh no"</code>。
|
||||
testString: assert("Ohhhh no".match(ohRegex)[0].length === 8);
|
||||
- text: 你的正则表达式应该匹配<code>"Ohhhhh no"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'Ohhhhh no'</code>。"
|
||||
testString: assert("Ohhhhh no".match(ohRegex)[0].length === 9);
|
||||
- text: 你的正则表达式应该匹配<code>"Ohhhhhh no"</code>
|
||||
- text: "你的正则表达式应该匹配<code>'Ohhhhhh no'</code>。"
|
||||
testString: assert("Ohhhhhh no".match(ohRegex)[0].length === 10);
|
||||
- text: 你的正则表达式不应该匹配<code>"Ohhhhhhh no"</code>
|
||||
- text: "你的正则表达式不应该匹配<code>'Ohhhhhhh no'</code>。"
|
||||
testString: assert(!ohRegex.test("Ohhhhhhh no"));
|
||||
|
||||
```
|
||||
@ -38,26 +53,24 @@ tests:
|
||||
|
||||
## 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);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let ohStr = "Ohhh no";
|
||||
let ohRegex = /Oh{3,6} no/; // Change this line
|
||||
let result = ohRegex.test(ohStr);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,24 +2,44 @@
|
||||
id: 587d7dbb367417b2b2512bab
|
||||
title: Use Capture Groups to Search and Replace
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
localeTitle: 使用捕获组进行搜索和替换
|
||||
forumTopicId: 301368
|
||||
localeTitle: 使用捕获组搜索和替换
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="description">搜索很有用。但是,当它也更改(或替换)您匹配的文本时,您可以使搜索功能更强大。您可以在字符串上使用<code>.replace()</code>搜索和替换字符串中的文本。 <code>.replace()</code>的输入首先是您要搜索的正则表达式模式。第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。 <blockquote> let wrongText =“天空是银色的。”; <br>让silverRegex = / silver /; <br> wrongText.replace(silverRegex,“blue”); <br> //返回“天空是蓝色的”。 </blockquote>您还可以使用美元符号( <code>$</code> )访问替换字符串中的捕获组。 <blockquote> “Code Camp”.replace(/(\ w +)\ s(\ w +)/,'$ 2 $ 1'); <br> //返回“营地代码” </blockquote></section>
|
||||
<section id='description'>
|
||||
搜索功能是很有用的。但是,当搜索同时也执行更改(或替换)匹配文本的操作时,搜索功能就会显得更加强大。
|
||||
可以使用字符串上<code>.replace()</code>方法来搜索并替换字符串中的文本。<code>.replace()</code>的输入首先是想要搜索的正则表达式匹配模式,第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。
|
||||
|
||||
```js
|
||||
let wrongText = "The sky is silver.";
|
||||
let silverRegex = /silver/;
|
||||
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
|
||||
<section id="instructions">写一个正则表达式,以便它搜索字符串<code>"good"</code> 。然后更新<code>replaceText</code>变量,将<code>"good"</code>替换为<code>"okey-dokey"</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
编写一个正则表达式,以搜索字符串<code>"good"</code>。然后更新变量<code>replaceText</code>,用字符串<code>"okey-dokey"</code>替换<code>"good"</code>。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 您应该使用<code>.replace()</code>来搜索和替换。
|
||||
- text: 你应该使用<code>.replace()</code>搜索并替换。
|
||||
testString: assert(code.match(/\.replace\(.*\)/));
|
||||
- text: 你的正则表达式应该改变<code>"This sandwich is good."</code> <code>"This sandwich is okey-dokey."</code>
|
||||
- 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\(.*?\)/));
|
||||
@ -38,7 +58,6 @@ let huhText = "This sandwich is good.";
|
||||
let fixRegex = /change/; // Change this line
|
||||
let replaceText = ""; // Change this line
|
||||
let result = huhText.replace(fixRegex, replaceText);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -51,6 +70,10 @@ let result = huhText.replace(fixRegex, replaceText);
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
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);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -2,24 +2,38 @@
|
||||
id: 587d7db3367417b2b2512b8e
|
||||
title: Using the Test Method
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301369
|
||||
localeTitle: 使用测试方法
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id="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> 。 <blockquote>让testStr =“freeCodeCamp”; <br>让testRegex = / Code /; <br> testRegex.test(testStr); <br> //返回true </blockquote></section>
|
||||
<section id='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>。
|
||||
|
||||
```js
|
||||
let testStr = "freeCodeCamp";
|
||||
let testRegex = /Code/;
|
||||
testRegex.test(testStr);
|
||||
// Returns true
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id="instructions">使用<code>.test()</code>方法在字符串<code>myString</code>上应用正则表达式<code>myRegex</code> 。 </section>
|
||||
<section id='instructions'>
|
||||
使用<code>.test()</code>方法,检测字符串<code>myString</code>是否符合正则表达式<code>myRegex</code>定义的规则。
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 你应该使用<code>.test()</code>来测试正则表达式。
|
||||
- text: 你应该使用<code>.test()</code>方法来检测正则表达式。
|
||||
testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
||||
- text: 您的结果应该返回<code>true</code> 。
|
||||
- text: 你的返回结果应该为<code>true</code>。
|
||||
testString: assert(result === true);
|
||||
|
||||
```
|
||||
@ -35,7 +49,6 @@ tests:
|
||||
let myString = "Hello, World!";
|
||||
let myRegex = /Hello/;
|
||||
let result = myRegex; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
@ -48,6 +61,9 @@ let result = myRegex; // Change this line
|
||||
<section id='solution'>
|
||||
|
||||
```js
|
||||
// solution required
|
||||
let myString = "Hello, World!";
|
||||
let myRegex = /Hello/;
|
||||
let result = myRegex.test(myString); // Change this line
|
||||
```
|
||||
|
||||
</section>
|
||||
|
Reference in New Issue
Block a user