diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-all-or-none.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-all-or-none.chinese.md index dd5e809d98..f9293d4410 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-all-or-none.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-all-or-none.chinese.md @@ -2,28 +2,43 @@ id: 587d7dba367417b2b2512ba8 title: Check for All or None challengeType: 1 -videoUrl: '' +forumTopicId: 301338 localeTitle: 检查全部或无 --- ## Description -
有时,您要搜索的模式可能包含可能存在或可能不存在的模式。但是,尽管如此,检查它们可能很重要。您可以指定可能存在带问号的元素, ? 。这将检查前一个元素中的零个或一个。您可以将此符号视为前一个元素是可选的。例如,美式英语和英式英语略有不同,您可以使用问号来匹配两种拼写。
让美国人=“颜色”;
让british =“color”;
让rainbowRegex = / colou?r /;
rainbowRegex.test(美国); //返回true
rainbowRegex.test(英国); //返回true
+
+有时,想要搜寻的匹配模式可能有不确定是否存在的部分。尽管如此,还是想检查它们。 +为此,可以使用问号?指定可能存在的元素。这将检查前面的零个或一个元素。可以将此符号视为前面的元素是可选的。 +例如,美式英语和英式英语略有不同,可以使用问号来匹配两种拼写。 + +```js +let american = "color"; +let british = "colour"; +let rainbowRegex= /colou?r/; +rainbowRegex.test(american); // Returns true +rainbowRegex.test(british); // Returns true +``` + +
## Instructions -
更改正则表达式favRegex以匹配该单词的美国英语(收藏)和英国英语(收藏)版本。
+
+修改正则表达式favRegex以匹配美式英语(favorite)和英式英语(favourite)的单词版本。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用可选的符号, ? 。 + - text: 你的正则表达式应该使用可选符号?。 testString: assert(favRegex.source.match(/\?/).length > 0); - - text: 你的正则表达式应该匹配"favorite" + - text: "你的正则表达式应该匹配'favorite'。" testString: assert(favRegex.test("favorite")); - - text: 你的正则表达式应该匹配"favourite" + - text: "你的正则表达式应该匹配'favourite'。" testString: assert(favRegex.test("favourite")); - - text: 你的正则表达式不应该匹配"fav" + - text: "你的正则表达式不应该匹配'fav'。" testString: assert(!favRegex.test("fav")); ``` @@ -39,7 +54,6 @@ tests: let favWord = "favorite"; let favRegex = /change/; // Change this line let result = favRegex.test(favWord); - ``` @@ -52,6 +66,9 @@ let result = favRegex.test(favWord);
```js -// solution required +let favWord = "favorite"; +let favRegex = /favou?r/; +let result = favRegex.test(favWord); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters.chinese.md new file mode 100644 index 0000000000..09c7d4e6ce --- /dev/null +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/check-for-mixed-grouping-of-characters.chinese.md @@ -0,0 +1,76 @@ +--- +id: 5c3dda8b4d8df89bea71600f +title: Check For Mixed Grouping of Characters +challengeType: 1 +forumTopicId: 301339 +localeTitle: 检查混合字符组 +--- + +## Description +
+有时候我们想使用正则表达式里的括号 () 来检查字符组。 +如果想在字符串找到 PenguinPumpkin,可以这个正则表达式:/P(engu|umpk)in/g。 +然后使用 test() 方法检查 test 字符串里面是否包含字符组。 + +```js +let testStr = "Pumpkin"; +let testRegex = /P(engu|umpk)in/g; +testRegex.test(testStr); +// Returns true +``` + +
+ +## Instructions +
+完善正则表达式,使其以区分大小写的方式检查 Franklin RooseveltEleanor Roosevelt 的名字,并且应该忽略 middle names。 +然后完善代码,使创建的正则检查 myString,根据正则是否匹配返回 truefalse。 +
+ +## Tests +
+ +```yml +tests: + - text: 正则 myRegex 测试 Franklin D. Roosevelt 应该返回 true。 + testString: myRegex.lastIndex = 0; assert(myRegex.test('Franklin D. Roosevelt')); + - text: 正则 myRegex 测试 Eleanor Roosevelt 应该返回 true。 + testString: myRegex.lastIndex = 0; assert(myRegex.test('Eleanor Roosevelt')); + - text: 正则 myRegex 测试 Franklin Rosevelt 应该返回 false。 + testString: myRegex.lastIndex = 0; assert(!myRegex.test('Franklin Rosevelt')); + - text: 应该使用 .test() 来测试正则。 + testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/)); + - text: result 应该返回 true。 + testString: assert(result === true); +``` + +
+ +## Challenge 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 +``` + +
+ + + +
+ +## Solution +
+ +```js +let myString = "Eleanor Roosevelt"; +let myRegex = /(Franklin|Eleanor).*Roosevelt/; +let result = myRegex.test(myString); +``` + +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/extract-matches.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/extract-matches.chinese.md index 8767835b8b..e54d9addba 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/extract-matches.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/extract-matches.chinese.md @@ -2,26 +2,41 @@ id: 587d7db4367417b2b2512b92 title: Extract Matches challengeType: 1 -videoUrl: '' -localeTitle: 提取匹配 +forumTopicId: 301340 +localeTitle: 提取匹配项 --- ## Description -
到目前为止,您只是检查字符串中是否存在模式。您还可以使用.match()方法提取您找到的实际匹配项。要使用.match()方法,请将该方法应用于字符串并传入括号内的正则表达式。这是一个例子:
“你好,世界!”。匹配(/ Hello /);
//返回[“Hello”]
让ourStr =“正则表达式”;
让ourRegex = / expressions /;
ourStr.match(ourRegex);
//返回[“表达式”]
+
+到目前为止,只是检查了一个匹配模式是否存在于字符串中。还可以使用.match()方法来提取找到的实际匹配项。 +可以使用字符串来调用.match()方法,并在括号内传入正则表达式。以下是一个示例: + +```js +"Hello, World!".match(/Hello/); +// Returns ["Hello"] +let ourStr = "Regular expressions"; +let ourRegex = /expressions/; +ourStr.match(ourRegex); +// Returns ["expressions"] +``` + +
## Instructions -
应用.match()方法来提取单词coding
+
+利用.match()方法提取单词coding。 +
## Tests
```yml tests: - - text: result应该有单词coding + - text: 结果应该包含单词coding。 testString: assert(result.join() === "coding"); - - text: 你的regex codingRegex应该搜索coding + - text: 你的正则表达式codingRegex应该搜寻coding。 testString: assert(codingRegex.source === "coding"); - - text: 您应该使用.match()方法。 + - text: 你应该使用.match()方法。 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 - ``` @@ -50,6 +64,9 @@ let result = extractStr; // Change this line
```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 ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-characters-with-lazy-matching.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-characters-with-lazy-matching.chinese.md index 678fa37a90..ab8248ca21 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-characters-with-lazy-matching.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-characters-with-lazy-matching.chinese.md @@ -2,23 +2,31 @@ id: 587d7db6367417b2b2512b9b title: Find Characters with Lazy Matching challengeType: 1 -videoUrl: '' -localeTitle: 查找具有延迟匹配的字符 +forumTopicId: 301341 +localeTitle: 用惰性匹配来查找字符 --- ## Description -
在正则表达式中, greedy匹配找到符合正则表达式模式的字符串的最长部分,并将其作为匹配返回。替代方案称为lazy匹配,它找到满足正则表达式模式的字符串的最小可能部分。您可以将regex /t[az]*i/应用于字符串"titanic" 。这个正则表达式基本上是一个以t开头的模式,以i结尾,并且在它们之间有一些字母。默认情况下,正则表达式是greedy ,因此匹配将返回["titani"] 。它找到可能适合模式的最大子串。但是,你可以使用?字符将其更改为lazy匹配。 "titanic"与调整后的正则表达式匹配/t[az]*?i/ return ["ti"]
+
+在正则表达式中,贪婪匹配会匹配到符合正则表达式匹配模式的字符串的最长可能部分,并将其作为匹配项返回。另一种方案称为懒惰匹配,它会匹配到满足正则表达式的字符串的最小可能部分。 +可以将正则表达式/t[a-z]*i/应用于字符串"titanic"。这个正则表达式是一个以t开始,以i结束,并且中间有一些字母的匹配模式。 +正则表达式默认是贪婪匹配,因此匹配返回为["titani"]。它会匹配到适合该匹配模式的最大子字符串。 +但是,你可以使用?字符来将其变成懒惰匹配。调整后的正则表达式/t[a-z]*?i/匹配字符串"titanic"返回["ti"]。 +注意
应该避免使用正则表达式解析 HTML,但是可以用正则表达式匹配 HTML 字符串。 +
## Instructions -
修复regex /<.*>/以返回HTML标记<h1>而不是文本"<h1>Winter is coming</h1>" 。记住通配符.在正则表达式中匹配任何字符。
+
+修复正则表达式/<.*>/,让它返回 HTML 标签<h1>,而不是文本"<h1>Winter is coming</h1>"。请记得在正则表达式中使用通配符.来匹配任意字符。 +
## Tests
```yml tests: - - text: result变量应该是一个包含<h1>的数组 - testString: 'assert(result[0] == "

", "The result variable should be an array with <h1> in it");' + - text: 结果变量应该是一个包含<h1>的数组。 + testString: assert(result[0] == '

'); ``` @@ -33,7 +41,6 @@ tests: let text = "

Winter is coming

"; let myRegex = /<.*>/; // Change this line let result = text.match(myRegex); - ``` @@ -46,6 +53,9 @@ let result = text.match(myRegex);
```js -// solution required +let text = "

Winter is coming

"; +let myRegex = /<.*?>/; // Change this line +let result = text.match(myRegex); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-more-than-the-first-match.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-more-than-the-first-match.chinese.md index 2192d10495..e8f0781708 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-more-than-the-first-match.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-more-than-the-first-match.chinese.md @@ -2,28 +2,49 @@ id: 587d7db4367417b2b2512b93 title: Find More Than the First Match challengeType: 1 -videoUrl: '' -localeTitle: 找到比第一场比赛更多的东西 +forumTopicId: 301342 +localeTitle: 全局匹配 --- ## Description -
到目前为止,您只能提取或搜索一次模式。
让testStr =“重复,重复,重复”;
让ourRegex = /重复/;
testStr.match(ourRegex);
//返回[“重复”]
要多次搜索或提取模式,可以使用g标志。
let repeatRegex = / Repeat / g;
testStr.match(repeatRegex);
//返回[“重复”,“重复”,“重复”]
+
+到目前为止,只能提取或搜寻一次模式匹配。 + +```js +let testStr = "Repeat, Repeat, Repeat"; +let ourRegex = /Repeat/; +testStr.match(ourRegex); +// Returns ["Repeat"] +``` + +若要多次搜寻或提取模式匹配,可以使用g标志。 + +```js +let repeatRegex = /Repeat/g; +testStr.match(repeatRegex); +// Returns ["Repeat", "Repeat", "Repeat"] +``` + +
## Instructions -
使用正则表达式starRegex ,找到并提取字符串twinkleStar "Twinkle"单词。 注意
你可以在你的正则表达式上有多个标志,比如/search/gi
+
+使用正则表达式starRegex,从字符串twinkleStar中匹配到所有的"Twinkle"单词并提取出来。 +注意:
在正则表达式上可以有多个标志,比如/search/gi。 +
## Tests
```yml tests: - - text: 你的正则表达式starRegex应该使用全局标志g + - text: 你的正则表达式starRegex应该使用全局标志g。 testString: assert(starRegex.flags.match(/g/).length == 1); - - text: 你的正则表达式starRegex应该使用不区分大小写的标志i + - text: 你的正则表达式starRegex应该使用忽略大小写标志i。 testString: assert(starRegex.flags.match(/i/).length == 1); - - text: 您的匹配应匹配"Twinkle"一词的出现次数 + - text: "你的匹配应该匹配单词'Twinkle'的两个匹配项。" testString: assert(result.sort().join() == twinkleStar.match(/twinkle/gi).sort().join()); - - text: 您的匹配result应该包含两个元素。 + - text: 你的匹配结果应该包含两个元素。 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 - ``` @@ -52,6 +72,9 @@ let result = twinkleStar; // Change this line
```js -// solution required +let twinkleStar = "Twinkle, twinkle, little star"; +let starRegex = /twinkle/gi; +let result = twinkleStar.match(starRegex); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-one-or-more-criminals-in-a-hunt.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-one-or-more-criminals-in-a-hunt.chinese.md index 578fa8f9bd..a7e1d296b6 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-one-or-more-criminals-in-a-hunt.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/find-one-or-more-criminals-in-a-hunt.chinese.md @@ -2,34 +2,57 @@ id: 587d7db7367417b2b2512b9c title: Find One or More Criminals in a Hunt challengeType: 1 -videoUrl: '' +forumTopicId: 301343 localeTitle: 在狩猎中找到一个或多个罪犯 --- ## Description -
是时候暂停和测试你的新正则表达式写作技巧了。一群罪犯逃出监狱逃跑,但你不知道有多少人。但是,你知道他们和其他人在一起时会保持紧密联系。你有责任立刻找到所有的罪犯。下面是一个查看如何执行此操作的示例:regex /z+/在连续出现一次或多次时匹配字母z 。它会在以下所有字符串中找到匹配项:
“Z”
“ZZZZZZ”
“ABCzzzz”
“zzzzABC”
“abczzzzzzzzzzzzzzzzzzzzzabc”
但它没有在以下字符串中找到匹配项,因为没有字母z字符:
“”
“ABC”
“ABCABC”
+
+是时候暂停和测试你的新正则表达式写作技巧了。一群罪犯逃出监狱逃跑,但你不知道有多少人。但是,你知道他们和其他人在一起时会保持紧密联系。你有责任立刻找到所有的罪犯。 +这里有一个示例来回顾如何做到这一点: +当字母z在一行中出现一次或连续多次时,正则表达式/z+/会匹配到它。它会在以下所有字符串中找到匹配项: + +```js +"z" +"zzzzzz" +"ABCzzzz" +"zzzzABC" +"abczzzzzzzzzzzzzzzzzzzzzabc" +``` + +但是它不会在以下字符串中找到匹配项,因为它们中没有字母z: + +```js +"" +"ABC" +"abcabc" +``` + +
## Instructions -
写一个greedy正则表达式,在一群其他人中找到一个或多个罪犯。罪犯由大写字母C
+
+编写一个贪婪正则表达式,在一组其他人中匹配到一个或多个罪犯。罪犯由大写字母C表示。 +
## Tests
```yml tests: - - text: 您正则表达式应该匹配one犯罪(“ C中”), "C" + - text: "你的正则表达式应该匹配'C'中的 一个 罪犯('C')。" testString: assert('C'.match(reCriminals) && 'C'.match(reCriminals)[0] == 'C'); - - text: 您正则表达式应该匹配two罪犯(“ CC中”) "CC" + - text: "你的正则表达式应该匹配'CC'中的 两个 罪犯('CC')。" testString: assert('CC'.match(reCriminals) && 'CC'.match(reCriminals)[0] == 'CC'); - - text: 你的正则表达式应匹配"P1P5P4CCCP2P6P3"中的three罪犯(“ CCC ”) + - text: "你的正则表达式应该匹配'P1P5P4CCCP2P6P3'中的 三个 罪犯('CCC')。" testString: assert('P1P5P4CCCP2P6P3'.match(reCriminals) && 'P1P5P4CCCP2P6P3'.match(reCriminals)[0] == 'CCC'); - - text: 你的正则表达式应匹配"P6P2P7P4P5CCCCCP3P1"中的five罪犯(“ CCCCC ”) + - text: "你的正则表达式应该匹配'P6P2P7P4P5CCCCCP3P1'中的 五个 罪犯('CCCCC')。" testString: assert('P6P2P7P4P5CCCCCP3P1'.match(reCriminals) && 'P6P2P7P4P5CCCCCP3P1'.match(reCriminals)[0] == 'CCCCC'); - - text: 你的正则表达式不应该匹配""中的任何罪犯 + - text: "你的正则表达式在''中不应该匹配到任何罪犯。" testString: assert(!reCriminals.test('')); - - text: 你的正则表达式不应该匹配"P1P2P3"中的任何罪犯 + - text: "你的正则表达式在'P1P2P3'中不应该匹配到任何罪犯。" testString: assert(!reCriminals.test('P1P2P3')); - - text: 您正则表达式应该与fifty的罪犯(“ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC中”) "P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3" 。 + - text: "你的正则表达式应该匹配'P2P1P5P4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCP3'中的 五十个 罪犯('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC')。" 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); - ``` @@ -62,6 +84,13 @@ console.log(matchedCriminals);
```js -// solution required +// example crowd gathering +let crowd = 'P1P2P3P4P5P6CCCP7P8P9'; + +let reCriminals = /C+/; // Change this line + +let matchedCriminals = crowd.match(reCriminals); + ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/ignore-case-while-matching.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/ignore-case-while-matching.chinese.md index 2d1637609f..296557c76c 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/ignore-case-while-matching.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/ignore-case-while-matching.chinese.md @@ -2,40 +2,46 @@ id: 587d7db4367417b2b2512b91 title: Ignore Case While Matching challengeType: 1 -videoUrl: '' +forumTopicId: 301344 localeTitle: 匹配时忽略大小写 --- ## Description -
到目前为止,你已经看过正则表达式来进行字符串的字面匹配。但有时,您可能还希望匹配案例差异。大小写(或有时是大写字母大小写)是大写字母和小写字母之间的区别。大写的示例是"A""B""C" 。小写的示例是"a""b""c" 。您可以使用所谓的标志来匹配这两种情况。还有其他标志,但在这里你将专注于忽略大小写的标志 - i旗帜。您可以通过将其附加到正则表达式来使用它。使用此标志的示例是/ignorecase/i 。此正则表达式可以匹配字符串"ignorecase""igNoreCase""IgnoreCase"
+
+到目前为止,已经了解了如何用正则表达式来执行字符串的匹配。但有时候,并不关注匹配字母的大小写。 +大小写即大写字母和小写字母。大写字母如"A""B""C"。小写字母如"a""b""c"。 +可以使用标志(flag)来匹配这两种情况。标志有很多,不过这里我们只关注忽略大小写的标志——i。可以通过将它附加到正则表达式之后来使用它。这里给出使用该标志的一个实例/ignorecase/i。这个字符串可以匹配字符串"ignorecase""igNoreCase""IgnoreCase"。 +
## Instructions -
写一个正则表达式fccRegex来匹配"freeCodeCamp" ,无论它的情况如何。您的正则表达式不应与任何缩写或带有空格的变体匹配。
+
+编写正则表达式fccRegex以匹配"freeCodeCamp",忽略大小写。正则表达式不应与任何缩写或带有空格的变体匹配。 +
## Tests
```yml tests: - - text: 你的正则表达式应该与freeCodeCamp匹配 + - text: 你的正则表达式应该匹配freeCodeCamp。 testString: assert(fccRegex.test('freeCodeCamp')); - - text: 你的正则表达式应该与FreeCodeCamp匹配 + - text: 你的正则表达式应该匹配FreeCodeCamp。 testString: assert(fccRegex.test('FreeCodeCamp')); - - text: 你的正则表达式应该与FreecodeCamp匹配 + - text: 你的正则表达式应该匹配FreecodeCamp。 testString: assert(fccRegex.test('FreecodeCamp')); - - text: 你的正则表达式应该与FreeCodecamp匹配 + - text: 你的正则表达式应该匹配FreeCodecamp。 testString: assert(fccRegex.test('FreeCodecamp')); - - text: 你的正则表达式不应该与Free Code Camp不匹配 + - text: 你的正则表达式不应该匹配Free Code Camp。 testString: assert(!fccRegex.test('Free Code Camp')); - - text: 你的正则表达式应该与FreeCOdeCamp匹配 + - text: Your regex should matchFreeCOdeCamp。 testString: assert(fccRegex.test('FreeCOdeCamp')); - - text: 你的正则表达式不应该与FCC匹配 + - text: 你的正则表达式不应该匹配FCC。 testString: assert(!fccRegex.test('FCC')); - - text: 你的正则表达式应该与FrEeCoDeCamp匹配 + - text: 你的正则表达式应该匹配FrEeCoDeCamp。 testString: assert(fccRegex.test('FrEeCoDeCamp')); - - text: 你的正则表达式应该与FrEeCodECamp匹配 + - text: 你的正则表达式应该匹配FrEeCodECamp。 testString: assert(fccRegex.test('FrEeCodECamp')); - - text: 你的正则表达式应该与FReeCodeCAmp匹配 + - text: 你的正则表达式应该匹配FReeCodeCAmp。 testString: assert(fccRegex.test('FReeCodeCAmp')); ``` @@ -51,7 +57,6 @@ tests: let myString = "freeCodeCamp"; let fccRegex = /change/; // Change this line let result = fccRegex.test(myString); - ``` @@ -64,6 +69,9 @@ let result = fccRegex.test(myString);
```js -// solution required +let myString = "freeCodeCamp"; +let fccRegex = /freecodecamp/i; // Change this line +let result = fccRegex.test(myString); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-a-literal-string-with-different-possibilities.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-a-literal-string-with-different-possibilities.chinese.md index 946c0a019e..a39d519ad0 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-a-literal-string-with-different-possibilities.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-a-literal-string-with-different-possibilities.chinese.md @@ -2,34 +2,41 @@ id: 587d7db4367417b2b2512b90 title: Match a Literal String with Different Possibilities challengeType: 1 -videoUrl: '' -localeTitle: 匹配具有不同可能性的文字字符串 +forumTopicId: 301345 +localeTitle: 同时用多种模式匹配文字字符串 --- ## Description -
使用/coding/等正则表达式,可以在另一个字符串中查找"coding"模式。这对搜索单个字符串很有用,但它仅限于一种模式。您可以使用alternationOR运算符搜索多个模式: | 。此运算符在其之前或之后匹配模式。例如,如果你想匹配"yes""no" ,你想要的正则表达式是/yes|no/ 。您还可以搜索两种以上的模式。您可以通过添加更多模式来实现此操作,其中更多OR运算符将它们分开,例如/yes|no|maybe/
+
+使用正则表达式/coding/,你可以在其他字符串中查找"coding"。 +这对于搜寻单个字符串非常有用,但仅限于一种匹配模式。你可以使用|操作符来匹配多个规则。 +此操作符匹配操作符前面或后面的字符。例如,如果你想匹配"yes""no",你需要的正则表达式是/yes|no/。 +你还可以匹配多个规则,这可以通过添加更多的匹配模式来实现。这些匹配模式将包含更多的|操作符来分隔它们,比如/yes|no|maybe/。 +
## Instructions -
完成正则表达式petRegex以匹配宠物"dog""cat""bird""fish"
+
+完成正则表达式petRegex以匹配"dog""cat""bird"或者"fish"。 +
## Tests
```yml tests: - - text: 你的正则表达式petRegex应该为字符串"John has a pet dog."返回true "John has a pet dog." + - text: "对于字符串'John has a pet dog.',你的正则表达式petRegextest方法应该返回true。" testString: assert(petRegex.test('John has a pet dog.')); - - text: 你的正则表达式petRegex应该为字符串"Emma has a pet rock."返回false "Emma has a pet rock." + - text: "对于字符串'Emma has a pet rock.',你的正则表达式petRegextest方法应该返回false。" testString: assert(!petRegex.test('Emma has a pet rock.')); - - text: 你的正则表达式petRegex应该为字符串"Emma has a pet bird."返回true "Emma has a pet bird." + - text: "对于字符串'Emma has a pet bird.',你的正则表达式petRegextest方法应该返回true。" testString: assert(petRegex.test('Emma has a pet bird.')); - - text: 你的正则表达式petRegex应该返回true为字符串"Liz has a pet cat." + - text: "对于字符串'Liz has a pet cat.',你的正则表达式petRegextest方法应该返回true。" testString: assert(petRegex.test('Liz has a pet cat.')); - - text: 你的正则表达式petRegex应该返回false"Kara has a pet dolphin."的字符串"Kara has a pet dolphin." + - text: "对于字符串'Kara has a pet dolphin.',你的正则表达式petRegextest方法应该返回false。" testString: assert(!petRegex.test('Kara has a pet dolphin.')); - - text: 你的正则表达式petRegex应该返回true为字符串"Alice has a pet fish." + - text: "对于字符串'Alice has a pet fish.',你的正则表达式petRegextest方法应该返回true。" testString: assert(petRegex.test('Alice has a pet fish.')); - - text: 你的正则表达式petRegex应该返回false为字符串"Jimmy has a pet computer." + - text: "对于字符串'Jimmy has a pet computer.',你的正则表达式petRegextest方法应该返回false。" 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); - ``` @@ -58,6 +64,9 @@ let result = petRegex.test(petString);
```js -// solution required +let petString = "James has a pet cat."; +let petRegex = /dog|cat|bird|fish/; // Change this line +let result = petRegex.test(petString); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-letters-and-numbers.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-letters-and-numbers.chinese.md index c4e5dca2f3..8c350ab134 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-letters-and-numbers.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-letters-and-numbers.chinese.md @@ -2,32 +2,49 @@ id: 587d7db7367417b2b2512b9f title: Match All Letters and Numbers challengeType: 1 -videoUrl: '' -localeTitle: 匹配所有字母和数字 +forumTopicId: 301346 +localeTitle: 匹配所有的字母和数字 --- ## Description -
使用字符类,您可以使用[az]搜索字母表中的所有字母。这种字符类很常见,它有一个快捷方式,虽然它还包含一些额外的字符。 JavaScript中与字母表匹配的最接近的字符类是\w 。此快捷方式等于[A-Za-z0-9_] 。此字符类匹配大写和小写字母加数字。注意,此字符类还包括下划线字符( _ )。
让longHand = / [A-Za-z0-9 _] + /;
让shortHand = / \ w + /;
让数字=“42”;
let varNames =“important_var”;
longHand.test(数字); //返回true
shortHand.test(数字); //返回true
longHand.test(varNames); //返回true
shortHand.test(varNames); //返回true
这些快捷方式字符类也称为shorthand character classes
+
+使用元字符,可以使用[a-z]搜寻字母表中的所有字母。这种元字符是很常见的,它有一个缩写,但这个缩写也包含额外的字符。 +JavaScript 中与字母表匹配的最接近的元字符是\w,这个缩写等同于[A-Za-z0-9_]。它不仅可以匹配大小写字母和数字,注意,它还会匹配下划线字符(_)。 + +```js +let longHand = /[A-Za-z0-9_]+/; +let shortHand = /\w+/; +let numbers = "42"; +let varNames = "important_var"; +longHand.test(numbers); // Returns true +shortHand.test(numbers); // Returns true +longHand.test(varNames); // Returns true +shortHand.test(varNames); // Returns true +``` + +
## Instructions -
使用速记字符类\w来计算各种引号和字符串中的字母数字字符数。
+
+使用缩写\w来计算所有引号中字母和数字字符的数量。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用全局标志。 + - text: 你的正则表达式应该使用全局状态修正符。 testString: assert(alphabetRegexV2.global); - - text: 你的正则表达式应该使用速记字符 + - text: 正则表达式应该使用元字符 \w 匹配字母表里的所有字符。 testString: assert(/\\w/.test(alphabetRegexV2.source)); - - text: 你的正则表达式应该在"The five boxing wizards jump quickly."找到31个字母数字字符"The five boxing wizards jump quickly." + - text: "你的正则表达式应该在'The five boxing wizards jump quickly.'中匹配到 31 个字母数字字符。" testString: assert("The five boxing wizards jump quickly.".match(alphabetRegexV2).length === 31); - - text: 你的正则表达式应该在"Pack my box with five dozen liquor jugs."找到32个字母数字字符"Pack my box with five dozen liquor jugs." + - text: "你的正则表达式应该在'Pack my box with five dozen liquor jugs.'中匹配到 32 个字母数字字符。" testString: assert("Pack my box with five dozen liquor jugs.".match(alphabetRegexV2).length === 32); - - text: 你的正则表达式应该在"How vexingly quick daft zebras jump!"找到30个字母数字字符"How vexingly quick daft zebras jump!" + - text: "你的正则表达式应该在'How vexingly quick daft zebras jump!'中匹配到 30 个字母数字字符。" testString: assert("How vexingly quick daft zebras jump!".match(alphabetRegexV2).length === 30); - - text: 你的正则表达式应该在"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."找到36个字母数字字符"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ." + - text: "你的正则表达式应该在'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'中匹配到 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; - ``` @@ -56,6 +72,9 @@ let result = quoteSample.match(alphabetRegexV2).length;
```js -// solution required +let quoteSample = "The five boxing wizards jump quickly."; +let alphabetRegexV2 = /\w/g; // Change this line +let result = quoteSample.match(alphabetRegexV2).length; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-non-numbers.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-non-numbers.chinese.md index 1ed91ec69a..2ced0f9a13 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-non-numbers.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-non-numbers.chinese.md @@ -2,36 +2,41 @@ id: 587d7db8367417b2b2512ba1 title: Match All Non-Numbers challengeType: 1 -videoUrl: '' +forumTopicId: 301347 localeTitle: 匹配所有非数字 --- ## Description -
最后一项挑战显示了如何使用带有小写d的快捷键\d来搜索数字。您也可以使用类似的使用大写D快捷方式搜索非数字。查找非数字字符的快捷方式是\D这等于字符类[^0-9] ,它查找不是0到9之间的数字的单个字符。
+
+上一项挑战中展示了如何使用带有小写d的缩写\d来搜寻数字。也可以使用类似的缩写来搜寻非数字,该缩写使用大写的D。 +查找非数字字符的缩写是\D。这等同于字符串[^0-9],它查找不是 0 - 9 之间数字的单个字符。 +
## Instructions -
使用非数字\D的速记字符类来计算电影标题中有多少个非数字。
+
+使用非数字缩写\D来计算电影标题中有多少非数字。 +
## Tests
```yml tests: - - text: 您的正则表达式应使用快捷方式字符来匹配非数字字符 + - text: 你的正则表达式应该使用缩写来匹配非数字字符。 testString: assert(/\\D/.test(noNumRegex.source)); - - text: 你的正则表达式应该使用全局标志。 + - text: 你的正则表达式应该使用全局状态修正符。 testString: assert(noNumRegex.global); - - text: 你的正则表达式应该在"9"找不到非数字。 + - text: "你的正则表达式在'9'中应该匹配不到非数字。" testString: assert("9".match(noNumRegex) == null); - - text: 你的正则表达式应该在"Catch 22"找到6个非数字。 + - text: "你的正则表达式应该在'Catch 22'中匹配到 6 个非数字。" testString: assert("Catch 22".match(noNumRegex).length == 6); - - text: 你的正则表达式应该在"101 Dalmatians"找到11个非数字。 + - text: "你的正则表达式应该在'101 Dalmatians'中匹配到 11 个非数字。" testString: assert("101 Dalmatians".match(noNumRegex).length == 11); - - text: '你的正则表达式应该在"One, Two, Three"找到15个非数字。' + - text: "你的正则表达式应该在'One, Two, Three'中匹配到 15 个非数字。" testString: assert("One, Two, Three".match(noNumRegex).length == 15); - - text: 你的正则表达式应该在"21 Jump Street"找到12个非数字。 + - text: "你的正则表达式应该在'21 Jump Street'中匹配到 12 个非数字。" testString: assert("21 Jump Street".match(noNumRegex).length == 12); - - text: '你的正则表达式应该在"2001: A Space Odyssey"找到17个非数字。' + - text: '你的正则表达式应该在"2001: A Space Odyssey"中匹配到 17 个非数字。' testString: 'assert("2001: A Space Odyssey".match(noNumRegex).length == 17);' ``` @@ -44,10 +49,9 @@ tests:
```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; ```
@@ -60,6 +64,9 @@ let result = numString.match(noNumRegex).length;
```js -// solution required +let movieName = "2001: A Space Odyssey"; +let noNumRegex = /\D/g; // Change this line +let result = movieName.match(noNumRegex).length; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-numbers.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-numbers.chinese.md index 20ebb47955..1bad4f1511 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-numbers.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-all-numbers.chinese.md @@ -2,36 +2,41 @@ id: 5d712346c441eddfaeb5bdef title: Match All Numbers challengeType: 1 -videoUrl: '' -localeTitle: 匹配所有号码 +forumTopicId: 18181 +localeTitle: 匹配所有数字 --- ## Description -
您已经学习了常用字符串模式(如字母数字)的快捷方式。另一种常见模式是寻找数字或数字。查找数字字符的快捷方式是\d ,小写d 。这等于字符类[0-9] ,它查找0到9之间任意数字的单个字符。
+
+已经了解了常见字符串匹配模式的元字符,如字母数字。另一个常见的匹配模式是只寻找数字。 +查找数字字符的缩写是\d,注意是小写的d。这等同于元字符[0-9],它查找 0 到 9 之间任意数字的单个字符。 +
## Instructions -
使用速记字符类\d来计算电影标题中的位数。写出的数字(“六”而不是六)不计算在内。
+
+使用缩写\d来计算电影标题中有多少个数字。书面数字("six" 而不是 6)不计算在内。 +
## Tests
```yml tests: - - text: 您的正则表达式应使用快捷方式字符来匹配数字字符 + - text: 你的正则表达式应该使用缩写来匹配数字字符。 testString: assert(/\\d/.test(numRegex.source)); - - text: 你的正则表达式应该使用全局标志。 + - text: 你的正则表达式应该使用全局状态修正符。 testString: assert(numRegex.global); - - text: 你的正则表达式应该在"9"找到1位数。 + - text: "你的正则表达式应该在'9'中匹配到 1 个数字。" testString: assert("9".match(numRegex).length == 1); - - text: 你的正则表达式应该在"Catch 22"找到2位数字。 + - text: "你的正则表达式应该在'Catch 22'中匹配到 2 个数字。" testString: assert("Catch 22".match(numRegex).length == 2); - - text: 你的正则表达式应该在"101 Dalmatians"找到3位数字。 + - text: "你的正则表达式应该在'101 Dalmatians'中匹配到 3 个数字。" testString: assert("101 Dalmatians".match(numRegex).length == 3); - - text: '你的正则表达式应该在"One, Two, Three"找不到数字。' + - text: "你的正则表达式在'One, Two, Three'中应该匹配不到数字。" testString: assert("One, Two, Three".match(numRegex) == null); - - text: 您的正则表达式应该在"21 Jump Street"找到2位数字。 + - text: "你的正则表达式应该在'21 Jump Street'中匹配到 2 个数字。" testString: assert("21 Jump Street".match(numRegex).length == 2); - - text: '你的正则表达式应该在"2001: A Space Odyssey"找到4位数字。' + - text: '你的正则表达式应该在"2001: A Space Odyssey"中匹配到 4 个数字。' testString: 'assert("2001: A Space Odyssey".match(numRegex).length == 4);' ``` @@ -44,10 +49,9 @@ tests:
```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; ```
@@ -60,6 +64,10 @@ let result = numString.match(numRegex).length;
```js -// solution required +let movieName = "2001: A Space Odyssey"; +let numRegex = /\d/g; // Change this line +let result = movieName.match(numRegex).length; + ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-anything-with-wildcard-period.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-anything-with-wildcard-period.chinese.md index 8eaab97c23..ab25e39e4c 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-anything-with-wildcard-period.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-anything-with-wildcard-period.chinese.md @@ -2,40 +2,54 @@ id: 587d7db5367417b2b2512b94 title: Match Anything with Wildcard Period challengeType: 1 -videoUrl: '' -localeTitle: 匹配通配符期间的任何内容 +forumTopicId: 301348 +localeTitle: 用通配符.匹配任何内容 --- ## Description -
有时您不会(或不需要)知道模式中的确切字符。想到所有匹配的单词,比如拼写错误需要很长时间。幸运的是,您可以使用通配符来节省时间: .通配符.将匹配任何一个字符。通配符也称为dotperiod 。您可以像使用正则表达式中的任何其他字符一样使用通配符。例如,如果你想匹配"hug""huh""hut""hum" ,你可以使用正则表达式/hu./来匹配所有四个单词。
让humStr =“我会哼唱一首歌”;
让hugStr =“熊抱”;
让huRegex = /hu./;
humStr.match(huRegex); //返回[“hum”]
hugStr.match(huRegex); //返回[“拥抱”]
+
+有时不(或不需要)知道匹配模式中的确切字符。如果要精确匹配到完整的单词,那出现一个拼写错误就会匹配不到。幸运的是,可以使用通配符.来处理这种情况。 +通配符.将匹配任何一个字符。通配符也叫dotperiod。可以像使用正则表达式中任何其他字符一样使用通配符。例如,如果想匹配"hug""huh""hut""hum",可以使用正则表达式/hu./匹配以上四个单词。 + +```js +let humStr = "I'll hum a song"; +let hugStr = "Bear hug"; +let huRegex = /hu./; +huRegex.test(humStr); // Returns true +huRegex.test(hugStr); // Returns true +``` + +
## Instructions -
完成正则表达式unRegex ,使其匹配字符串"run""sun""fun""pun""nun""bun" 。你的正则表达式应该使用通配符。
+
+完成正则表达式unRegex以匹配字符串"run""sun""fun""pun""nun""bun"。正则表达式中应该使用通配符。 +
## Tests
```yml tests: - - text: 您应该使用.test()方法。 + - text: 你应该使用.test()方法。 testString: assert(code.match(/\.test\(.*\)/)); - - text: 您应该在正则表达式unRegex使用通配符 + - text: 你应该在你的正则表达式unRegex中使用通配符。 testString: assert(/\./.test(unRegex.source)); - - text: 你的正则表达式unRegex应该匹配"run" "Let us go on a run." "run"中的"run" "Let us go on a run." + - text: "你的正则表达式unRegex应该在字符串'Let us go on a run.'中匹配到'run'单词。" testString: assert(unRegex.test("Let us go on a run.")); - - text: 你的正则表达式unRegex应该与"sun" "The sun is out today." "sun"中的"sun"匹配"The sun is out today." + - text: "你的正则表达式unRegex应该在字符串'The sun is out today.'中匹配到'sun'单词。" testString: assert(unRegex.test("The sun is out today.")); - - text: 你的正则表达式unRegex应该与"fun" "Coding is a lot of fun." "fun"中的"fun"匹配"Coding is a lot of fun." + - text: "你的正则表达式unRegex应该在字符串'Coding is a lot of fun.'中匹配到'fun'单词。" testString: assert(unRegex.test("Coding is a lot of fun.")); - - text: 你的正则表达式unRegex应该匹配"pun" "Seven days without a pun makes one weak." "pun" "Seven days without a pun makes one weak." + - text: "你的正则表达式unRegex应该在字符串'Seven days without a pun makes one weak.'中匹配到'pun'单词。" testString: assert(unRegex.test("Seven days without a pun makes one weak.")); - - text: 你的正则表达式unRegex应该与"nun" "One takes a vow to be a nun." "nun"中的"nun"匹配"One takes a vow to be a nun." + - text: "你的正则表达式unRegex应该在字符串'One takes a vow to be a nun.'中匹配到'nun'单词。" testString: assert(unRegex.test("One takes a vow to be a nun.")); - - text: 你的正则表达式unRegex应该匹配"bun" "She got fired from the hot dog stand for putting her hair in a bun." "bun"中的"bun" "She got fired from the hot dog stand for putting her hair in a bun." + - text: "你的正则表达式unRegex应该在字符串'She got fired from the hot dog stand for putting her hair in a bun.'中匹配到'bun'单词。" testString: assert(unRegex.test("She got fired from the hot dog stand for putting her hair in a bun.")); - - text: 您的正则表达式unRegex不应与"There is a bug in my code."匹配"There is a bug in my code." + - text: "你的正则表达式unRegex不应该匹配'There is a bug in my code.'。" testString: assert(!unRegex.test("There is a bug in my code.")); - - text: 您的正则表达式unRegex不应该匹配"Catch me if you can." + - text: "你的正则表达式unRegex不应该匹配'Catch me if you can.'。" 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); - ``` @@ -64,6 +77,9 @@ let result = unRegex.test(exampleStr);
```js -// solution required +let exampleStr = "Let's have fun with regular expressions!"; +let unRegex = /.un/; // Change this line +let result = unRegex.test(exampleStr); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-beginning-string-patterns.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-beginning-string-patterns.chinese.md index 99eb6afbf9..17b835086d 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-beginning-string-patterns.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-beginning-string-patterns.chinese.md @@ -2,28 +2,44 @@ id: 587d7db7367417b2b2512b9d title: Match Beginning String Patterns challengeType: 1 -videoUrl: '' -localeTitle: 匹配开始字符串模式 +forumTopicId: 301349 +localeTitle: 匹配字符串的开头 --- ## Description -
先前的挑战表明,正则表达式可用于寻找许多匹配。它们还用于搜索字符串中特定位置的模式。在之前的挑战中,您使用character set内的caret符( ^ )来创建[^thingsThatWillNotBeMatched]形式的negated character set 。在character setcaret用于在字符串的开头搜索模式。
让firstString =“Ricky是第一个,可以找到。”;
让firstRegex = / ^ Ricky /;
firstRegex.test(firstString);
//返回true
让notFirst =“你现在找不到Ricky了。”;
firstRegex.test(notFirst);
//返回false
+
+回顾一下之前的挑战,正则表达式可以用于查找多项匹配。还可以查询字符串中符合指定匹配模式的字符。 +在之前的挑战中,使用字符集中的插入符号(^)来创建一个否定字符集,形如[^thingsThatWillNotBeMatched]。在字符集之外,插入符号用于字符串的开头搜寻匹配模式。 + +```js +let firstString = "Ricky is first and can be found."; +let firstRegex = /^Ricky/; +firstRegex.test(firstString); +// Returns true +let notFirst = "You can't find Ricky now."; +firstRegex.test(notFirst); +// Returns false +``` + +
## Instructions -
使用正则表达式中的caret只能在字符串rickyAndCal的开头找到"Cal"
+
+在正则表达式中使用^符号,以匹配仅在字符串rickyAndCal的开头出现的"Cal"。 +
## Tests
```yml tests: - - text: 你的正则表达式应该用大写字母搜索"Cal" 。 + - text: "你的正则表达式应该搜寻有一个大写字母的'Cal'。" testString: assert(calRegex.source == "^Cal"); - text: 你的正则表达式不应该使用任何标志。 testString: assert(calRegex.flags == ""); - - text: 你的正则表达式应该匹配字符串开头的"Cal" 。 + - text: "你的正则表达式应该匹配字符串开头的'Cal'。" testString: assert(calRegex.test("Cal and Ricky both like racing.")); - - text: 您的正则表达式不应与字符串中间的"Cal"匹配。 + - text: "你的正则表达式不应该匹配字符串中间的'Cal'。" 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); - ``` @@ -52,6 +67,9 @@ let result = calRegex.test(rickyAndCal);
```js -// solution required +let rickyAndCal = "Cal and Ricky both like racing."; +let calRegex = /^Cal/; // Change this line +let result = calRegex.test(rickyAndCal); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-one-or-more-times.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-one-or-more-times.chinese.md index e56dfb0de9..062784a99b 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-one-or-more-times.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-one-or-more-times.chinese.md @@ -2,15 +2,22 @@ id: 587d7db6367417b2b2512b99 title: Match Characters that Occur One or More Times challengeType: 1 -videoUrl: '' +forumTopicId: 301350 localeTitle: 匹配出现一次或多次的字符 --- ## Description -
有时,您需要匹配连续出现一次或多次的字符(或字符组)。这意味着它至少发生一次,并且可以重复。您可以使用+字符来检查是否是这种情况。请记住,角色或模式必须连续出现。也就是说,角色必须一个接一个地重复。例如, /a+/g会在"abc"找到一个匹配并返回["a"] 。由于+ ,它也会在"aabc"找到一个匹配并返回["aa"] 。如果它是检查字符串"abab" ,它会找到两个匹配并返回["a", "a"]因为a字符不在一行 - 它们之间有一个b 。最后,由于字符串"bcd"没有"a" "bcd" ,因此找不到匹配项。
+
+有时,需要匹配出现一次或者连续多次的的字符(或字符组)。这意味着它至少出现一次,并且可能重复出现。 +可以使用+符号来检查情况是否如此。记住,字符或匹配模式必须一个接一个地连续出现。 +例如,/a+/g会在"abc"中匹配到一个匹配项,并且返回["a"]。因为+的存在,它也会在"aabc"中匹配到一个匹配项,然后返回["aa"]。 +如果它是检查字符串"abab",它将匹配到两个匹配项并且返回["a", "a"],因为a字符不连续,在它们之间有一个b字符。最后,因为在字符串"bcd"中没有"a",因此找不到匹配项。 +
## Instructions -
您希望在"Mississippi"字母s出现一次或多次时找到匹配项。写一个使用+符号的正则表达式。
+
+想要在字符串"Mississippi"中匹配到出现一次或多次的字母s的匹配项。编写一个使用+符号的正则表达式。 +
## Tests
@@ -19,9 +26,9 @@ localeTitle: 匹配出现一次或多次的字符 tests: - text: 你的正则表达式myRegex应该使用+符号来匹配一个或多个s字符。 testString: assert(/\+/.test(myRegex.source)); - - text: 你的正则表达式myRegex应该匹配2个项目。 + - text: 你的正则表达式myRegex应该匹配两项。 testString: assert(result.length == 2); - - text: result变量应该是一个包含两个匹配"ss"的数组 + - text: "结果变量应该是一个包含两个'ss'匹配项的数组。" 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); - ``` @@ -50,6 +56,9 @@ let result = difficultSpelling.match(myRegex);
```js -// solution required +let difficultSpelling = "Mississippi"; +let myRegex = /s+/g; // Change this line +let result = difficultSpelling.match(myRegex); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-zero-or-more-times.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-zero-or-more-times.chinese.md index 728a7fb179..e5cde2c654 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-zero-or-more-times.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-zero-or-more-times.chinese.md @@ -2,31 +2,49 @@ id: 587d7db6367417b2b2512b9a title: Match Characters that Occur Zero or More Times challengeType: 1 -videoUrl: '' +forumTopicId: 301351 localeTitle: 匹配出现零次或多次的字符 --- ## Description -
最后一项挑战使用加+号来查找出现一次或多次的字符。还有一个选项可以匹配出现零次或多次的字符。执行此操作的字符是asteriskstar asterisk*
让soccerWord =“gooooooooal!”;
让gPhrase =“直觉”;
让oPhrase =“越过月亮”;
let goRegex = / go * /;
soccerWord.match(goRegex); //返回[“goooooooo”]
gPhrase.match(goRegex); //返回[“g”]
oPhrase.match(goRegex); //返回null
+
+上一次的挑战中使用了加号+来查找出现一次或多次的字符。还有一个选项可以匹配出现零次或多次的字符。 +执行该操作的字符叫做asteriskstar,即*。 + +```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 +``` + +
## Instructions -
创建一个正则表达式chewieRegex使用的*字符匹配所有上下"a"中的字符chewieQuote 。你的正则表达式不需要标志,它不应该与任何其他引号相匹配。
+
+在这个挑战里,chewieQuote 已经被初始化为 "Aaaaaaaaaaaaaaaarrrgh!"。创建一个变量为chewieRegex的正则表达式,使用*符号在chewieQuote中匹配"A"及其之后出现的零个或多个"a"。你的正则表达式不需要使用修饰符,也不需要匹配引号。 +
## Tests
```yml tests: - - text: 您正则表达式chewieRegex应该使用*字符匹配零个或多个a字符。 + - text: "你的正则表达式chewieRegex应该使用*符号匹配'A'之后出现的零个或多个'a'字符。" testString: assert(/\*/.test(chewieRegex.source)); - - text: 你的正则表达式chewieRegex应匹配16个字符。 + - text: 正则表达式应当匹配 chewieQuote 里的 "A"。 + testString: assert(result[0][0] === 'A'); + - text: "你的正则表达式应该匹配'Aaaaaaaaaaaaaaaa'。" + testString: assert(result[0] === 'Aaaaaaaaaaaaaaaa'); + - text: 你的正则表达式chewieRegex应该匹配 16 个字符。 testString: assert(result[0].length === 16); - - text: 你的正则表达式应该匹配"Aaaaaaaaaaaaaaaa" 。 - testString: assert(result[0] === "Aaaaaaaaaaaaaaaa"); - - text: '你的正则表达式不应该与"He made a fair move. Screaming about it can't help you."中的任何角色相匹配"He made a fair move. Screaming about it can't help you."' - testString: assert(!"He made a fair move. Screaming about it can\"t help you.".match(chewieRegex)); - - text: '你的正则表达式不应该与"Let him have it. It's not wise to upset a Wookiee."中的任何角色相匹配"Let him have it. It's not wise to upset a Wookiee."' - testString: assert(!"Let him have it. It\"s not wise to upset a Wookiee.".match(chewieRegex)); + - text: "你的正则表达式在'He made a fair move. Screaming about it can't help you.'中不应该匹配任何字符。" + testString: assert(!"He made a fair move. Screaming about it can't help you.".match(chewieRegex)); + - text: "你的正则表达式在'Let him have it. It's not wise to upset a Wookiee.'中不应该匹配任何字符。" + testString: assert(!"Let him have it. It's not wise to upset a Wookiee.".match(chewieRegex)); ``` @@ -38,15 +56,20 @@ tests:
```js -let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!"; -let chewieRegex = /change/; // Change this line +let chewieRegex = /change/; // Only change this line let result = chewieQuote.match(chewieRegex); - ```
+## Before Test +
+```js +const chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!"; +``` + +
@@ -54,6 +77,8 @@ let result = chewieQuote.match(chewieRegex);
```js -// solution required + let chewieRegex = /Aa*/; + let result = chewieQuote.match(chewieRegex); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-ending-string-patterns.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-ending-string-patterns.chinese.md index 9b60c346f7..2309b6d037 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-ending-string-patterns.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-ending-string-patterns.chinese.md @@ -2,26 +2,43 @@ id: 587d7db7367417b2b2512b9e title: Match Ending String Patterns challengeType: 1 -videoUrl: '' -localeTitle: 匹配结束字符串模式 +forumTopicId: 301352 +localeTitle: 匹配字符串的末尾 --- ## Description -
在上一个挑战中,您学会了使用caret来搜索字符串开头的模式。还有一种方法可以在字符串末尾搜索模式。您可以使用正则表达式末尾的dollar sign字符$来搜索字符串的结尾。
让theEnding =“这是一个永无止境的故事”;
让storyRegex = / story $ /;
storyRegex.test(theEnding);
//返回true
让noEnding =“有时故事必须结束”;
storyRegex.test(noEnding);
//返回false
+
+在上一个挑战中,学习了使用^符号来搜寻字符串开头的匹配模式。还有一种方法可以搜寻字符串末尾的匹配模式。 +可以使用正则表达式的美元符号$来搜寻字符串的结尾。 + +```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 + +``` + +
## Instructions -
使用锚字符( $ )来匹配字符串"caboose"在字符串的结尾caboose
+
+使用$在字符串caboose的末尾匹配"caboose"。 +
## Tests
```yml tests: - - text: 您应该在正则表达式中使用美元符号$ anchor搜索"caboose" 。 + - text: "你应该在正则表达式使用美元符号$来搜寻'caboose'。" testString: assert(lastRegex.source == "caboose$"); - text: 你的正则表达式不应该使用任何标志。 testString: assert(lastRegex.flags == ""); - - text: 您应该在字符串末尾匹配"caboose" "The last car on a train is the caboose" + - text: "你应该在字符串'The last car on a train is the caboose'的末尾匹配'caboose'。" 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); - ``` @@ -50,6 +66,9 @@ let result = lastRegex.test(caboose);
```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); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-everything-but-letters-and-numbers.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-everything-but-letters-and-numbers.chinese.md index b32e9df7d9..b1d85af496 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-everything-but-letters-and-numbers.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-everything-but-letters-and-numbers.chinese.md @@ -2,32 +2,46 @@ id: 587d7db8367417b2b2512ba0 title: Match Everything But Letters and Numbers challengeType: 1 -videoUrl: '' -localeTitle: 匹配一切,但字母和数字 +forumTopicId: 301353 +localeTitle: 匹配除了字母和数字的所有符号 --- ## Description -
您已经了解到可以使用快捷键来匹配使用\w字母数字[A-Za-z0-9_] 。您可能想要搜索的自然模式与字母数字相反。您可以使用\W搜索\w的反面。注意,相反的模式使用大写字母。此快捷方式与[^A-Za-z0-9_]
让shortHand = / \ W /;
让数字=“42%”;
let sentence =“Coding!”;
numbers.match(简写); //返回[“%”]
sentence.match(简写); //返回[“!”]
+
+已经了解到可以使用缩写\w来匹配字母和数字[A-Za-z0-9_]。不过,有可能想要搜寻的匹配模式是非字母数字字符。 +可以使用\W搜寻和\w相反的匹配模式。注意,相反匹配模式使用大写字母。此缩写与[^A-Za-z0-9_]是一样的。 + +```js +let shortHand = /\W/; +let numbers = "42%"; +let sentence = "Coding!"; +numbers.match(shortHand); // Returns ["%"] +sentence.match(shortHand); // Returns ["!"] +``` + +
## Instructions -
使用速记字符类\W来计算各种引号和字符串中的非字母数字字符数。
+
+使用缩写\W来计算不同引号和字符串中非字母数字字符的数量。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用全局标志。 + - text: 你的正则表达式应该使用全局状态修正符。 testString: assert(nonAlphabetRegex.global); - - text: 你的正则表达式应该在"The five boxing wizards jump quickly."找到6个非字母数字字符"The five boxing wizards jump quickly." 。 + - text: "你的正则表达式应该在'The five boxing wizards jump quickly.'中匹配到 6 个非字母数字字符。" testString: assert("The five boxing wizards jump quickly.".match(nonAlphabetRegex).length == 6); - - text: 你的正则表达式应该使用速记字符。 + - text: 正则表达式应该使用元字符来匹配非字母字符。 testString: assert(/\\W/.test(nonAlphabetRegex.source)); - - text: 你的正则表达式应该在"Pack my box with five dozen liquor jugs."找到8个非字母数字字符"Pack my box with five dozen liquor jugs." + - text: "你的正则表达式应该在'Pack my box with five dozen liquor jugs.'中匹配到 8 个非字母数字字符。" testString: assert("Pack my box with five dozen liquor jugs.".match(nonAlphabetRegex).length == 8); - - text: 你的正则表达式应该在"How vexingly quick daft zebras jump!"找到6个非字母数字字符"How vexingly quick daft zebras jump!" + - text: "你的正则表达式应该在'How vexingly quick daft zebras jump!'中匹配到 6 个非字母数字字符。" testString: assert("How vexingly quick daft zebras jump!".match(nonAlphabetRegex).length == 6); - - text: 你的正则表达式应该在"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."找到12个非字母数字字符"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ." + - text: "你的正则表达式应该在'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'中匹配到 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; - ``` @@ -56,6 +69,9 @@ let result = quoteSample.match(nonAlphabetRegex).length;
```js -// solution required +let quoteSample = "The five boxing wizards_jump quickly."; +let nonAlphabetRegex = /\W/g; // Change this line +let result = quoteSample.match(nonAlphabetRegex).length; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-letters-of-the-alphabet.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-letters-of-the-alphabet.chinese.md index 19a2680c06..c00aafad3d 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-letters-of-the-alphabet.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-letters-of-the-alphabet.chinese.md @@ -2,26 +2,44 @@ id: 587d7db5367417b2b2512b96 title: Match Letters of the Alphabet challengeType: 1 -videoUrl: '' -localeTitle: 匹配字母的字母 +forumTopicId: 301354 +localeTitle: 匹配字母表中的字母 --- ## Description -
您了解了如何使用character sets来指定要匹配的一组字符,但是当您需要匹配大范围的字符(例如,字母表中的每个字母)时,这是很多类型。幸运的是,有一个内置功能,使这简短。在character set ,您可以使用hyphen字符来定义要匹配的hyphen范围: - 。例如,要匹配小写字母ae您将使用[ae]
让catStr =“猫”;
让batStr =“蝙蝠”;
让matStr =“mat”;
让bgRegex = / [ae] at /;
catStr.match(bgRegex); //返回[“cat”]
batStr.match(bgRegex); //返回[“bat”]
matStr.match(bgRegex); //返回null
+
+了解了如何使用字符集来指定要匹配的一组字符串,但是当需要匹配大量字符(例如,字母表中的每个字母)时,有一种写法可以让实现这个功能变得简短。 +在字符集中,可以使用连字符-)来定义要匹配的字符范围。 +例如,要匹配小写字母ae,你可以使用[a-e]。 + +```js +let catStr = "cat"; +let batStr = "bat"; +let matStr = "mat"; +let bgRegex = /[a-e]at/; +catStr.match(bgRegex); // Returns ["cat"] +batStr.match(bgRegex); // Returns ["bat"] +matStr.match(bgRegex); // Returns null +``` + +
## Instructions -
匹配字符串quoteSample中的所有字母。 注意
务必匹配大写和小写字母
+
+匹配字符串quoteSample中的所有字母。 +注意:
一定要同时匹配大小写字母。 +
## Tests
```yml tests: - - text: 你的正则表达式alphabetRegex应该匹配35项。 + - text: 你的正则表达式alphabetRegex应该匹配 35 项。 testString: assert(result.length == 35); - text: 你的正则表达式alphabetRegex应该使用全局标志。 testString: assert(alphabetRegex.flags.match(/g/).length == 1); - - text: 你的正则表达式alphabetRegex应该使用不区分大小写的标志。 + - text: 你的正则表达式alphabetRegex应该使用忽略大小写标志。 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 - ``` @@ -50,6 +67,9 @@ let result = alphabetRegex; // Change this line
```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 ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-literal-strings.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-literal-strings.chinese.md index ed6e4b6e8e..7e769c8332 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-literal-strings.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-literal-strings.chinese.md @@ -2,26 +2,47 @@ id: 587d7db3367417b2b2512b8f title: Match Literal Strings challengeType: 1 -videoUrl: '' +forumTopicId: 301355 localeTitle: 匹配文字字符串 --- ## Description -
在上一次挑战中,您使用正则表达式/Hello/搜索了单词"Hello" 。该正则表达式搜索字符串"Hello"的文字匹配。这是另一个搜索字符串"Kevin"的文字匹配的示例:
让testStr =“你好,我的名字是凯文。”;
让testRegex = / Kevin /;
testRegex.test(testStr);
//返回true
任何其他形式的"Kevin"都不匹配。例如,正则表达式/Kevin/将不匹配"kevin""KEVIN"
let wrongRegex = / kevin /;
wrongRegex.test(testStr);
//返回false
未来的挑战将展示如何匹配其他形式。
+
+在上一个挑战中,使用正则表达式/Hello/搜索到了字符串"Hello"。那个正则表达式在字符串中搜寻"Hello"的文字匹配。下面是另一个在字符串中搜寻"Kevin"的示例: + +```js +let testStr = "Hello, my name is Kevin."; +let testRegex = /Kevin/; +testRegex.test(testStr); +// Returns true +``` + +任何其他形式的"Kevin"都不会被匹配。例如,正则表达式/Kevin/不会匹配"kevin"或者"KEVIN"。 + +```js +let wrongRegex = /kevin/; +wrongRegex.test(testStr); +// Returns false +``` + +后续的挑战将为你展示如何匹配其他形式的字符串。 +
## Instructions -
完成正则表达式waldoRegex在字符串waldoIsHiding使用文字匹配查找"Waldo"
+
+完成正则表达式waldoRegex,在字符串waldoIsHiding中匹配到文本"Waldo"。 +
## Tests
```yml tests: - - text: 你的正则表达式waldoRegex应该找到"Waldo" + - text: "你的正则表达式waldoRegex应该匹配到'Waldo'。" testString: assert(waldoRegex.test(waldoIsHiding)); - - text: 你的正则表达式waldoRegex不应该搜索任何其他内容。 + - text: 你的正则表达式waldoRegex不应该搜寻其他的任何内容。 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); - ``` @@ -50,6 +70,9 @@ let result = waldoRegex.test(waldoIsHiding);
```js -// solution required +let waldoIsHiding = "Somewhere Waldo is hiding in this text."; +let waldoRegex = /Waldo/; // Change this line +let result = waldoRegex.test(waldoIsHiding); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-non-whitespace-characters.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-non-whitespace-characters.chinese.md index 5d06a082d2..7ef6802763 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-non-whitespace-characters.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-non-whitespace-characters.chinese.md @@ -2,30 +2,42 @@ id: 587d7db9367417b2b2512ba4 title: Match Non-Whitespace Characters challengeType: 1 -videoUrl: '' +forumTopicId: 18210 localeTitle: 匹配非空白字符 --- ## Description -
您学会了使用\s搜索空格,并使用小写s 。您还可以搜索除空格之外的所有内容。使用\S搜索非空格, \S是一个大写的s 。此模式将不匹配空格,回车符,制表符,换页符和换行符。你可以认为它类似于字符类[^ \r\t\f\n\v]
让whiteSpace =“空白。到处都是空白!”
让nonSpaceRegex = / \ S / g;
whiteSpace.match(nonSpaceRegex)。长度; //返回32
+
+已经学会了如何使用带有小写s的缩写\s来搜寻空白字符。还可以搜寻除了空格之外的所有内容。 +使用\S搜寻非空白字符,其中S是大写。此匹配模式将不匹配空格、回车符、制表符、换页符和换行符。可以认为这类似于元字符[^\r\t\f\n\v]。 + +```js +let whiteSpace = "Whitespace. Whitespace everywhere!" +let nonSpaceRegex = /\S/g; +whiteSpace.match(nonSpaceRegex).length; // Returns 32 +``` + +
## Instructions -
更改正则表达式countNonWhiteSpace以在字符串中查找多个非空白字符。
+
+修改正则表达式countNonWhiteSpace以查找字符串中的多个非空字符。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用全局标志。 + - text: 你的正则表达式应该使用全局状态修正符。 testString: assert(countNonWhiteSpace.global); - - text: 你的正则表达式应该使用速记字符 + - text: 正则表达式应该使用元字符 \S/code> 来匹配所有的非空格字符。 testString: assert(/\\S/.test(countNonWhiteSpace.source)); - - text: 你的正则表达式应该在"Men are from Mars and women are from Venus."找到35个非空格"Men are from Mars and women are from Venus." + - text: "你的正则表达式应该在'Men are from Mars and women are from Venus.'中匹配到 35 个非空白字符。" testString: assert("Men are from Mars and women are from Venus.".match(countNonWhiteSpace).length == 35); - - text: '你的正则表达式应该在"Space: the final frontier."找到23个非空格"Space: the final frontier."' + - text: '你的正则表达式应该在"Space: the final frontier."中匹配到 23 个非空白字符。' testString: 'assert("Space: the final frontier.".match(countNonWhiteSpace).length == 23);' - - text: 你的正则表达式应该在"MindYourPersonalSpace"找到21个非空格 + - text: "你的正则表达式应该在'MindYourPersonalSpace'中匹配到 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); - ``` @@ -54,6 +65,9 @@ let result = sample.match(countNonWhiteSpace);
```js -// solution required +let sample = "Whitespace is important in separating words"; +let countNonWhiteSpace = /\S/g; // Change this line +let result = sample.match(countNonWhiteSpace); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-numbers-and-letters-of-the-alphabet.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-numbers-and-letters-of-the-alphabet.chinese.md index 83018df411..a79f79385f 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-numbers-and-letters-of-the-alphabet.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-numbers-and-letters-of-the-alphabet.chinese.md @@ -2,26 +2,40 @@ id: 587d7db5367417b2b2512b97 title: Match Numbers and Letters of the Alphabet challengeType: 1 -videoUrl: '' -localeTitle: 匹配数字和字母的字母 +forumTopicId: 301356 +localeTitle: 匹配字母表中的数字和字母 --- ## Description -
使用连字符( - )匹配一系列字符不仅限于字母。它也适用于匹配一系列数字。例如, /[0-5]/匹配05之间的任何数字,包括05 。此外,可以在单个字符集中组合一系列字母和数字。
让jennyStr =“Jenny8675309”;
让myRegex = / [a-z0-9] / ig;
//匹配jennyStr中的所有字母和数字
jennyStr.match(myRegex);
+
+使用连字符(-)匹配字符范围并不仅限于字母。它还可以匹配一系列数字。 +例如,/[0-5]/匹配05之间的任意数字,包含05。 +此外,还可以在单个字符集中组合一系列字母和数字。 + +```js +let jennyStr = "Jenny8675309"; +let myRegex = /[a-z0-9]/ig; +// matches all letters and numbers in jennyStr +jennyStr.match(myRegex); +``` + +
## Instructions -
创建一个与hs之间的字母范围匹配的正则表达式,以及介于26之间的数字范围。请记住在正则表达式中包含适当的标志。
+
+创建一个正则表达式,使其可以匹配hs之间的一系列字母,以及26之间的一系列数字。请记得在正则表达式中包含恰当的标志。 +
## Tests
```yml tests: - - text: 你的正则表达式myRegex应该匹配17项。 + - text: 你的正则表达式myRegex应该匹配 17 项。 testString: assert(result.length == 17); - text: 你的正则表达式myRegex应该使用全局标志。 testString: assert(myRegex.flags.match(/g/).length == 1); - - text: 你的正则表达式myRegex应该使用不区分大小写的标志。 + - text: 你的正则表达式myRegex应该使用忽略大小写的标志。 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 - ``` @@ -50,6 +63,10 @@ let result = myRegex; // Change this line
```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 + ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-single-character-with-multiple-possibilities.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-single-character-with-multiple-possibilities.chinese.md index 47ef6237a4..711a62b414 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-single-character-with-multiple-possibilities.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-single-character-with-multiple-possibilities.chinese.md @@ -2,30 +2,50 @@ id: 587d7db5367417b2b2512b95 title: Match Single Character with Multiple Possibilities challengeType: 1 -videoUrl: '' -localeTitle: 将单个角色与多种可能性相匹配 +forumTopicId: 301357 +localeTitle: 将单个字符与多种可能性匹配 --- ## Description -
您学习了如何匹配文字模式( /literal/ )和通配符( /./ )。这些是正则表达式的极端,其中一个找到完全匹配,另一个匹配一切。有两个极端之间可以平衡的选项。您可以使用character classes搜索具有一定灵活性的文字模式。字符类允许您通过将它们放在方括号( [] )括号内来定义要匹配的一组字符。例如,您想匹配"bag""big""bug"但不匹配"bog" 。您可以创建regex /b[aiu]g/来执行此操作。 [aiu]是仅匹配字符"a""i""u"的字符类。
让bigStr =“大”;
让bagStr =“bag”;
让bugStr =“bug”;
让bogStr =“bog”;
让bgRegex = / b [aiu] g /;
bigStr.match(bgRegex); //返回[“大”]
bagStr.match(bgRegex); //返回[“bag”]
bugStr.match(bgRegex); //返回[“bug”]
bogStr.match(bgRegex); //返回null
+
+已经了解了文字匹配模式(/literal/)和通配符(/./)。这是正则表达式的两种极端情况,一种是精确匹配,而另一种则是匹配所有。在这两种极端情况之间有一个平衡选项。 +可以使用字符集搜寻具有一定灵活性的文字匹配模式。可以把字符集放在方括号([])之间来定义一组需要匹配的字符串。 +例如,如果想要匹配"bag""big""bug",但是不想匹配"bog"。可以创建正则表达式/b[aiu]g/来执行此操作。[aiu]是只匹配字符"a""i"或者"u"的字符集。 + +```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 +``` + +
## Instructions -
在正则表达式vowelRegex使用带元音( aeiou )的字符类来查找字符串quoteSample中的所有元音。 注意
确保匹配大写和小写元音。
+
+使用元音字符集(aeiou)在正则表达式vowelRegex中匹配到字符串quoteSample中的所有元音。 +注意
一定要同时匹配大小写元音。 +
## Tests
```yml tests: - - text: 你应该找到所有25个元音。 + - text: 你应该匹配到所有25个元音。 testString: assert(result.length == 25); - - text: 你的正则表达式vowelRegex应该使用一个字符类。 + - text: 你的正则表达式vowelRegex应该使用字符集。 testString: assert(/\[.*\]/.test(vowelRegex.source)); - text: 你的正则表达式vowelRegex应该使用全局标志。 testString: assert(vowelRegex.flags.match(/g/).length == 1); - - text: 你的正则表达式vowelRegex应该使用不区分大小写的标志。 + - text: 你的正则表达式vowelRegex应该使用忽略大小写标志。 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 - ``` @@ -54,6 +73,9 @@ let result = vowelRegex; // Change this line
```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 ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-single-characters-not-specified.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-single-characters-not-specified.chinese.md index b5fb7cf90a..db96f07b2f 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-single-characters-not-specified.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-single-characters-not-specified.chinese.md @@ -2,26 +2,32 @@ id: 587d7db6367417b2b2512b98 title: Match Single Characters Not Specified challengeType: 1 -videoUrl: '' -localeTitle: 匹配未指定的单个字符 +forumTopicId: 301358 +localeTitle: 匹配单个未指定的字符 --- ## Description -
到目前为止,您已创建了一组要匹配的字符,但您也可以创建一组您不想匹配的字符。这些类型的字符集称为negated character sets 。要创建negated character set ,请在caret括号后面和不想匹配的字符前放置一个caret^ )。例如, /[^aeiou]/gi匹配所有不是元音的字符。请注意字符之类的.![@/和空格匹配 - 否定元音字符集仅排除元音字符。
+
+到目前为止,已经创建了一个想要匹配的字符集合,但也可以创建一个不想匹配的字符集合。这些类型的字符集称为否定字符集。 +要创建否定字符集,需要在开始括号后面和不想匹配的字符前面放置插入字符(即^)。 +例如,/[^aeiou]/gi匹配所有非元音字符。注意,字符.![@/和空白字符等也会被匹配,该否定字符集仅排除元音字符。 +
## Instructions -
创建一个匹配所有不是数字或元音的字符的正则表达式。请记住在正则表达式中包含适当的标志。
+
+创建一个匹配所有非数字或元音字符的正则表达式。请记得在正则表达式中包含恰当的标志。 +
## Tests
```yml tests: - - text: 你的正则表达式myRegex应匹配9项。 + - text: 你的正则表达式myRegex应该匹配 9 项。 testString: assert(result.length == 9); - text: 你的正则表达式myRegex应该使用全局标志。 testString: assert(myRegex.flags.match(/g/).length == 1); - - text: 你的正则表达式myRegex应该使用不区分大小写的标志。 + - text: 你的正则表达式myRegex应该使用忽略大小写标志。 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 - ``` @@ -50,6 +55,9 @@ let result = myRegex; // Change this line
```js -// solution required +let quoteSample = "3 blind mice."; +let myRegex = /[^0-9aeiou]/gi; // Change this line +let result = quoteSample.match(myRegex); // Change this line ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-whitespace.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-whitespace.chinese.md index 72ed291067..25eba921be 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-whitespace.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/match-whitespace.chinese.md @@ -2,30 +2,43 @@ id: 587d7db8367417b2b2512ba3 title: Match Whitespace challengeType: 1 -videoUrl: '' -localeTitle: 匹配空白 +forumTopicId: 301359 +localeTitle: 匹配空白字符 --- ## Description -
迄今为止的挑战包括匹配的字母和数字字母。您还可以匹配字母之间的空格或空格。您可以使用\s搜索空格,这是一个小写的s 。此模式不仅匹配空格,还匹配回车符,制表符,换页符和换行符。你可以认为它类似于字符类[ \r\t\f\n\v]
让whiteSpace =“空白。到处都是空白!”
让spaceRegex = / \ s / g;
whiteSpace.match(spaceRegex);
//返回[“”,“”]
+
+迄今为止的挑战包括匹配的字母和数字。还可以匹配字母之间的空格。 +可以使用\s搜寻空格,其中s是小写。此匹配模式不仅匹配空格,还匹配回车符、制表符、换页符和换行符,可以将其视为与[\r\t\f\n\v]类似。 + +```js +let whiteSpace = "Whitespace. Whitespace everywhere!" +let spaceRegex = /\s/g; +whiteSpace.match(spaceRegex); +// Returns [" ", " "] +``` + +
## Instructions -
更改正则表达式countWhiteSpace以查找字符串中的多个空格字符。
+
+修改正则表达式countWhiteSpace查找字符串中的多个空白字符。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用全局标志。 + - text: 你的正则表达式应该使用全局状态修正符。 testString: assert(countWhiteSpace.global); - - text: 你的正则表达式应该使用速记字符 + - text: 正则表达式应该使用元字符 \s 匹配所有的空白。 testString: assert(/\\s/.test(countWhiteSpace.source)); - - text: 你的正则表达式应该在"Men are from Mars and women are from Venus."找到八个空格"Men are from Mars and women are from Venus." + - text: "你的正则表达式应该在'Men are from Mars and women are from Venus.'中匹配到 8 个空白字符。" testString: assert("Men are from Mars and women are from Venus.".match(countWhiteSpace).length == 8); - - text: '你的正则表达式应该在"Space: the final frontier."找到三个空格"Space: the final frontier."' + - text: '你的正则表达式应该在"Space: the final frontier."中匹配到 3 个空白字符。' testString: 'assert("Space: the final frontier.".match(countWhiteSpace).length == 3);' - - text: 您的正则表达式应该在"MindYourPersonalSpace"中找不到空格 + - text: "你的正则表达式在'MindYourPersonalSpace'中应该匹配不到空白字符。" 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); - ``` @@ -54,6 +66,9 @@ let result = sample.match(countWhiteSpace);
```js -// solution required +let sample = "Whitespace is important in separating words"; +let countWhiteSpace = /\s/g; +let result = sample.match(countWhiteSpace); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead.chinese.md index 8259110993..a564d424e0 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead.chinese.md @@ -2,37 +2,69 @@ id: 587d7dba367417b2b2512ba9 title: Positive and Negative Lookahead challengeType: 1 -videoUrl: '' -localeTitle: 积极和消极的前瞻 +forumTopicId: 301360 +localeTitle: 正向先行断言和负向先行断言 --- ## Description -
Lookaheads是一种模式,它告诉JavaScript在字符串中向前看以进一步检查模式。当您想要在同一个字符串上搜索多个模式时,这非常有用。有两种lookaheadspositive lookaheadnegative lookahead 。一个positive lookahead向前看将确保搜索模式中的元素存在,但实际上不匹配它。正向前瞻用作(?=...) ,其中...是不匹配的必需部分。另一方面, negative lookahead将确保搜索模式中的元素不存在。负向前瞻用作(?!...) ,其中...是您不希望在那里的模式。如果不存在负前瞻部分,则返回模式的其余部分。前瞻有点令人困惑,但一些例子会有所帮助。
让quit =“qu”;
让noquit =“qt”;
让quRegex = / q(?= u)/;
让qRegex = / q(?!u)/;
quit.match(quRegex); //返回[“q”]
noquit.match(qRegex); //返回[“q”]
lookaheads更实际用途是检查一个字符串中的两个或更多个模式。这是一个(天真)简单的密码检查器,可以查找3到6个字符和至少一个数字:
let password =“abc123”;
让checkPass = /(?= \ w {3,6})(?= \ D * \ d)/;
checkPass.test(密码); //返回true
+
+先行断言是告诉 JavaScript 在字符串中向前查找的匹配模式。当想要在同一个字符串上搜寻多个匹配模式时,这可能非常有用。 +有两种先行断言正向先行断言负向先行断言。 +正向先行断言会查看并确保搜索匹配模式中的元素存在,但实际上并不匹配。正向先行断言的用法是(?=...),其中...就是需要存在但不会被匹配的部分。 +另一方面,负向先行断言会查看并确保搜索匹配模式中的元素不存在。负向先行断言的用法是(?!...),其中...是希望不存在的匹配模式。如果负向先行断言部分不存在,将返回匹配模式的其余部分。 +尽管先行断言有点儿令人困惑,但是这些示例会有所帮助。 + +```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"] +``` + +先行断言的更实际用途是检查一个字符串中的两个或更多匹配模式。这里有一个简单的密码检查器,密码规则是 3 到 6 个字符且至少包含一个数字: + +```js +let password = "abc123"; +let checkPass = /(?=\w{3,6})(?=\D*\d)/; +checkPass.test(password); // Returns true +``` + +
## Instructions -
使用lookaheadspwRegex匹配长的时间大于5个字符,并有两个连续的数字密码。
+
+在正则表达式pwRegex中使用先行断言以匹配大于5个字符且有两个连续数字的密码,并且不能以数字开头。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用两个积极的lookaheads 。 + - text: 你的正则表达式应该使用两个正向先行断言。 testString: assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null); - - text: 你的正则表达式不应该匹配"astronaut" + - text: "你的正则表达式不应该匹配'astronaut'。" testString: assert(!pwRegex.test("astronaut")); - - text: 你的正则表达式不应该与"airplanes"匹配 + - text: "你的正则表达式不应该匹配'airplanes'。" testString: assert(!pwRegex.test("airplanes")); - - text: 你的正则表达式不应该匹配"banan1" + - text: 正则不应该匹配 "banan1" testString: assert(!pwRegex.test("banan1")); - - text: 你的正则表达式应该匹配"bana12" + - text: "你的正则表达式应该匹配'bana12'。" testString: assert(pwRegex.test("bana12")); - - text: 你的正则表达式应该匹配"abc123" + - text: "你的正则表达式应该匹配'abc123'。" testString: assert(pwRegex.test("abc123")); - - text: 你的正则表达式不应该匹配"123" + - text: "你的正则表达式不应该匹配'123'。" testString: assert(!pwRegex.test("123")); - - text: 你的正则表达式不应该匹配"1234" + - text: "你的正则表达式不应该匹配'1234'。" testString: assert(!pwRegex.test("1234")); + - text: 正则不应该匹配 "8pass99" + testString: assert(!pwRegex.test("8pass99")); + - text: 正则不应该匹配 "12abcde" + testString: assert(!pwRegex.test("12abcde")); + + ``` @@ -47,7 +79,6 @@ tests: let sampleWord = "astronaut"; let pwRegex = /change/; // Change this line let result = pwRegex.test(sampleWord); - ``` @@ -59,7 +90,9 @@ let result = pwRegex.test(sampleWord); ## Solution
+ ```js -// solution required +var pwRegex = /^(?=\w{6})(?=\D+\d{2})/; ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end.chinese.md index 0be4b80612..c16a602b7a 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end.chinese.md @@ -2,26 +2,31 @@ id: 587d7dbb367417b2b2512bac title: Remove Whitespace from Start and End challengeType: 1 -videoUrl: '' -localeTitle: 从开始和结束中删除空格 +forumTopicId: 301362 +localeTitle: 删除开头和结尾的空白 --- ## Description -
有时字符串周围的空白字符不是必需的,而是存在的。字符串的典型处理是删除字符串开头和结尾处的空格。
+
+有时字符串周围存在的空白字符并不是必需的。字符串的典型处理是删除字符串开头和结尾处的空格。 +
## Instructions -
编写一个正则表达式并使用适当的字符串方法删除字符串开头和结尾的空格。 注意
.trim()方法可以在这里工作,但您需要使用正则表达式完成此挑战。
+
+编写一个正则表达式并使用适当的字符串方法删除字符串开头和结尾的空格。 +注意:
.trim()方法在这里也可以实现同样的效果,但是你需要使用正则表达式来完成此项挑战。 +
## Tests
```yml tests: - - text: 'result应该等于"Hello, World!"' + - text: "结果应该等于'Hello, World!'。" testString: assert(result == "Hello, World!"); - - text: 您不应该使用.trim()方法。 - testString: assert(!code.match(/\.trim\([\s\S]*?\)/)); - - text: result变量不应设置为等于字符串。 + - text: 你不应该使用.trim()方法。 + testString: assert(!code.match(/\.trim\(.*?\)/)); + - text: 结果变量不应该设置为等于字符串。 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 - ``` @@ -50,6 +54,9 @@ let result = hello; // Change this line
```js -// solution required +let hello = " Hello, World! "; +let wsRegex = /^(\s+)(.+[^\s])(\s+)$/; +let result = hello.replace(wsRegex, '$2'); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames.chinese.md index aadf9c6621..1709673529 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames.chinese.md @@ -2,33 +2,52 @@ id: 587d7db8367417b2b2512ba2 title: Restrict Possible Usernames challengeType: 1 -videoUrl: '' +forumTopicId: 301363 localeTitle: 限制可能的用户名 --- ## Description -
用户名在互联网上随处可见。它们是用户在自己喜欢的网站上获得独特身份的原因。您需要检查数据库中的所有用户名。以下是用户在创建用户名时必须遵循的一些简单规则。 1)用户名中的唯一数字必须在最后。最后可以有零个或多个。 2)用户名字母可以是小写和大写。 3)用户名必须至少两个字符长。双字母用户名只能使用字母字母。
+
+用户名在互联网上随处可见。它们是用户在自己喜欢的网站上的唯一身份。 +需要检索数据库中的所有用户名。以下是用户在创建用户名时必须遵守的一些简单规则。 +1) 用户名只能是数字字母字符。 +2) 用户名中的数字必须在最后,且数字可以有零个或多个。 +3) 用户名字母可以是小写字母和大写字母。 +4) 用户名长度必须至少为两个字符。两位用户名只能使用字母。 +
## Instructions -
更改正则表达式userCheck以适合上面列出的约束。
+
+修改正则表达式userCheck以适合上面列出的约束。 +
## Tests
```yml tests: - - text: 你的正则表达式应该与JACK匹配 - testString: 'assert(userCheck.test("JACK"), "Your regex should match JACK");' - - text: 你的正则表达式不应该与J匹配 - testString: 'assert(!userCheck.test("J"), "Your regex should not match J");' - - text: 你的正则表达式应该与Oceans11匹配 - testString: 'assert(userCheck.test("Oceans11"), "Your regex should match Oceans11");' - - text: 你的正则表达式应该与RegexGuru匹配 - testString: 'assert(userCheck.test("RegexGuru"), "Your regex should match RegexGuru");' - - text: 你的正则表达式不应该与007匹配 - testString: 'assert(!userCheck.test("007"), "Your regex should not match 007");' - - text: 你的正则表达式不应该匹配9 - testString: 'assert(!userCheck.test("9"), "Your regex should not match 9");' + - text: 你的正则表达式应该匹配JACK。 + testString: assert(userCheck.test("JACK")); + - text: 你的正则表达式不应该匹配J。 + testString: assert(!userCheck.test("J")); + - text: 正则表达式应该匹配 Jo。 + testString: assert(userCheck.test("Jo")); + - text: 你的正则表达式应该匹配Oceans11。 + testString: assert(userCheck.test("Oceans11")); + - text: 你的正则表达式应该匹配RegexGuru。 + testString: assert(userCheck.test("RegexGuru")); + - text: 你的正则表达式不应该匹配007。 + testString: assert(!userCheck.test("007")); + - text: 你的正则表达式不应该匹配9。 + testString: assert(!userCheck.test("9")); + - text: 正则表达式不应该匹配 A1。 + testString: assert(!userCheck.test("A1")); + - text: 正则表达式不应该匹配 BadUs3rnam3。 + testString: assert(!userCheck.test("BadUs3rnam3")); + - text: 正则表达式应该匹配 Z97。 + testString: assert(userCheck.test("Z97")); + - text: 正则表达式不应该匹配 c57bT3。 + testString: assert(!userCheck.test("c57bT3")); ``` @@ -43,7 +62,6 @@ tests: let username = "JackOfAllTrades"; let userCheck = /change/; // Change this line let result = userCheck.test(username); - ``` @@ -56,6 +74,9 @@ let result = userCheck.test(username);
```js -// solution required +let username = "JackOfAllTrades"; +const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i; +let result = userCheck.test(username); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups.chinese.md index 4c6ae7056d..856b7715d2 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups.chinese.md @@ -2,40 +2,56 @@ id: 587d7dbb367417b2b2512baa title: Reuse Patterns Using Capture Groups challengeType: 1 -videoUrl: '' +forumTopicId: 301364 localeTitle: 使用捕获组重用模式 --- ## Description -
您搜索的某些模式将在字符串中多次出现。手动重复该正则表达式是浪费的。有一种更好的方法可以指定何时在字符串中有多个重复子字符串。您可以使用capture groups搜索重复子字符串。括号()用于查找重复子串。你把模式的正则表达式重复在括号之间。要指定重复字符串的显示位置,请使用反斜杠( \ ),然后使用数字。此数字从1开始,随着您使用的每个其他捕获组而增加。一个例子是\1来匹配第一组。下面的示例匹配以空格分隔的两次出现的任何单词:
让repeatStr =“正则表达式正则表达式”;
let repeatRegex = /(\ w +)\ s \ 1 /;
repeatRegex.test(repeatStr); //返回true
repeatStr.match(repeatRegex); //返回[“regex regex”,“regex”]
对字符串使用.match()方法将返回一个数组,其中包含与其匹配的字符串及其捕获组。
+
+一些你所搜寻的匹配模式会在字符串中出现多次,手动重复该正则表达式太浪费了。有一种更好的方法可以指定何时在字符串中会有多个重复的子字符串。 +可以使用捕获组搜寻重复的子字符串。括号()可以用来匹配重复的子字符串。只需要把重复匹配模式的正则表达式放在括号中即可。 +要指定重复字符串将出现的位置,可以使用反斜杠(\)后接一个数字。这个数字从 1 开始,随着你使用的每个捕获组的增加而增加。这里有一个示例,\1可以匹配第一个组。 +下面的示例匹配任意两个被空格分割的单词: + +```js +let repeatStr = "regex regex"; +let repeatRegex = /(\w+)\s\1/; +repeatRegex.test(repeatStr); // Returns true +repeatStr.match(repeatRegex); // Returns ["regex regex", "regex"] +``` + +在字符串上使用.match()方法将返回一个数组,其中包含它匹配的字符串及其捕获组。 +
## Instructions -
reRegex使用capture groups来匹配在字符串中仅重复三次的数字,每个数字用空格分隔。
+
+在正则表达式reRegex中使用捕获组,以匹配在字符串中仅重复三次的数字,每一个都由空格分隔。 +
## 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: 你的正则表达式应该匹配"42 42 42" 。 + - text: "你的正则表达式应该匹配'42 42 42'。" testString: assert(reRegex.test("42 42 42")); - - text: 你的正则表达式应该匹配"100 100 100" 。 + - text: "你的正则表达式应该匹配'100 100 100'。" testString: assert(reRegex.test("100 100 100")); - - text: 你的正则表达式不应该匹配"42 42 42 42" 。 + - text: "你的正则表达式不应该匹配'42 42 42 42'。" testString: assert.equal(("42 42 42 42").match(reRegex.source), null); - - text: 你的正则表达式不应该匹配"42 42" 。 + - text: "你的正则表达式不应该匹配'42 42'。" testString: assert.equal(("42 42").match(reRegex.source), null); - - text: 你的正则表达式不应该匹配"101 102 103" 。 + - text: "你的正则表达式不应该匹配'101 102 103'。" testString: assert(!reRegex.test("101 102 103")); - - text: 你的正则表达式不应该匹配"1 2 3" 。 + - text: "你的正则表达式不应该匹配'1 2 3'。" testString: assert(!reRegex.test("1 2 3")); - - text: 你的正则表达式应匹配"10 10 10" 。 + - text: "你的正则表达式应该匹配'10 10 10'。" 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); - ``` @@ -64,6 +79,9 @@ let result = reRegex.test(repeatNum);
```js -// solution required +let repeatNum = "42 42 42"; +let reRegex = /^(\d+)\s\1\s\1$/; +let result = reRegex.test(repeatNum); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-exact-number-of-matches.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-exact-number-of-matches.chinese.md index 4d0be9ba21..672bf4fb0d 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-exact-number-of-matches.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-exact-number-of-matches.chinese.md @@ -2,32 +2,49 @@ id: 587d7db9367417b2b2512ba7 title: Specify Exact Number of Matches challengeType: 1 -videoUrl: '' -localeTitle: 指定完全匹配数 +forumTopicId: 301365 +localeTitle: 指定匹配的确切数量 --- ## Description -
您可以使用大括号quantity specifiers的较低和较高数量的模式。有时您只需要特定数量的匹配。要指定一定数量的模式,只需在大括号之间放置一个数字。例如,要仅将单词"hah"与字母a匹配3次,您的正则表达式将为/ha{3}h/
让A4 =“haaaah”;
让A3 =“haaah”;
设A100 =“h”+“a”.repeat(100)+“h”;
let multipleHA = / ha {3} h /;
multipleHA.test(A4); //返回false
multipleHA.test(A3); //返回true
multipleHA.test(A100); //返回false
+
+可以使用带有花括号的数量说明符来指定匹配模式的上下限。但有时只需要特定数量的匹配。 +要指定一定数量的匹配模式,只需在大括号之间放置一个数字。 +例如,要只匹配字母a出现3次的单词"hah",正则表达式应为/ha{3}h/。 + +```js +let A4 = "haaaah"; +let A3 = "haaah"; +let A100 = "h" + "a".repeat(100) + "h"; +let multipleHA = /ha{3}h/; +multipleHA.test(A4); // Returns false +multipleHA.test(A3); // Returns true +multipleHA.test(A100); // Returns false +``` + +
## Instructions -
只有当它有四个字母m时才更改正则表达式timRegex以匹配单词"Timber"
+
+修改正则表达式timRegex,以匹配仅有四个字母单词m的单词"Timber"。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用大括号。 + - text: 你的正则表达式应该使用花括号。 testString: assert(timRegex.source.match(/{.*?}/).length > 0); - - text: 你的正则表达式不应该与"Timber"匹配 + - text: "你的正则表达式不应该匹配'Timber'。" testString: assert(!timRegex.test("Timber")); - - text: 你的正则表达式不应该匹配"Timmber" + - text: "你的正则表达式不应该匹配'Timmber'。" testString: assert(!timRegex.test("Timmber")); - - text: 你的正则表达式不应该匹配"Timmmber" + - text: "你的正则表达式不应该匹配'Timmmber'。" testString: assert(!timRegex.test("Timmmber")); - - text: 你的正则表达式应该匹配"Timmmmber" + - text: "你的正则表达式应该匹配'Timmmmber'。" testString: assert(timRegex.test("Timmmmber")); - - text: 你的正则表达式不应该与30 m"Timber"相匹配。 + - text: "你的正则表达式不应该匹配包含 30 个字母m'Timber'。" 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); - ``` @@ -56,6 +72,9 @@ let result = timRegex.test(timStr);
```js -// solution required +let timStr = "Timmmmber"; +let timRegex = /Tim{4}ber/; // Change this line +let result = timRegex.test(timStr); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-only-the-lower-number-of-matches.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-only-the-lower-number-of-matches.chinese.md index 902c4b0550..2c14e93038 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-only-the-lower-number-of-matches.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-only-the-lower-number-of-matches.chinese.md @@ -2,34 +2,51 @@ id: 587d7db9367417b2b2512ba6 title: Specify Only the Lower Number of Matches challengeType: 1 -videoUrl: '' -localeTitle: 仅指定较低的匹配数 +forumTopicId: 301366 +localeTitle: 只指定匹配的下限 --- ## Description -
您可以使用大括号quantity specifiers的较低和较高数量的模式。有时您只想指定较低数量的模式而没有上限。要仅指定较少的模式数,请保留第一个数字后跟逗号。例如,要仅匹配字符串"hah"与出现至少3次的字母a ,您的正则表达式将是/ha{3,}h/
让A4 =“haaaah”;
让A2 =“哈哈”;
设A100 =“h”+“a”.repeat(100)+“h”;
let multipleA = / ha {3,} h /;
multipleA.test(A4); //返回true
multipleA.test(A2); //返回false
multipleA.test(A100); //返回true
+
+可以使用带有花括号的数量说明符来指定匹配模式的上下限。但有时候只想指定匹配模式的下限而不需要指定上限。 +为此,在第一个数字后面跟一个逗号即可。 +例如,要匹配至少出现3次字母a的字符串"hah",正则表达式应该是/ha{3,}h/。 + +```js +let A4 = "haaaah"; +let A2 = "haah"; +let A100 = "h" + "a".repeat(100) + "h"; +let multipleA = /ha{3,}h/; +multipleA.test(A4); // Returns true +multipleA.test(A2); // Returns false +multipleA.test(A100); // Returns true +``` + +
## Instructions -
只有当它有四个或更多字母z时才更改正则表达式haRegex以匹配单词"Hazzah"
+
+修改正则表达式haRegex,匹配包含四个或更多字母z的单词"Hazzah"。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用大括号。 + - text: 你的正则表达式应该使用花括号。 testString: assert(haRegex.source.match(/{.*?}/).length > 0); - - text: 你的正则表达式不应该与"Hazzah"匹配 + - text: "你的正则表达式不应该匹配'Hazzah'。" testString: assert(!haRegex.test("Hazzah")); - - text: 你的正则表达式不应该与"Hazzzah"匹配 + - text: "你的正则表达式不应该匹配'Hazzzah'。" testString: assert(!haRegex.test("Hazzzah")); - - text: 你的正则表达应该匹配"Hazzzzah" + - text: 正则表达式应该匹配 "Hazzzzah" testString: assert("Hazzzzah".match(haRegex)[0].length === 8); - - text: 你的正则表达应该匹配"Hazzzzzah" + - text: "你的正则表达式应该匹配'Hazzzzah'。" testString: assert("Hazzzzzah".match(haRegex)[0].length === 9); - - text: 你的正则表达应该匹配"Hazzzzzzah" + - text: 正则表达式应该匹配 "Hazzzzzzah" testString: assert("Hazzzzzzah".match(haRegex)[0].length === 10); - - text: 你的正则表达式应该匹配"Hazzah"和30个z 。 + - text: 正则表达式应该匹配 "Hazzah" with 30 z'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); - ``` @@ -58,6 +74,9 @@ let result = haRegex.test(haStr);
```js -// solution required +let haStr = "Hazzzzah"; +let haRegex = /Haz{4,}ah/; // Change this line +let result = haRegex.test(haStr); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-upper-and-lower-number-of-matches.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-upper-and-lower-number-of-matches.chinese.md index f3a3ba6635..ff5e3e4ff6 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-upper-and-lower-number-of-matches.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/specify-upper-and-lower-number-of-matches.chinese.md @@ -2,34 +2,49 @@ id: 587d7db9367417b2b2512ba5 title: Specify Upper and Lower Number of Matches challengeType: 1 -videoUrl: '' -localeTitle: 指定上下匹配数 +forumTopicId: 301367 +localeTitle: 指定匹配的上限和下限 --- ## Description -
回想一下,您使用加号+来查找一个或多个字符,使用星号*来查找零个或多个字符。这些很方便,但有时你想要匹配一定范围的模式。您可以使用quantity specifiers模式的下限和上限。数量说明符与大括号( {} )一起使用。您在大括号之间放置了两个数字 - 用于较低和较高的模式数。例如,为了匹配字母"ah"出现35次的字母a ,你的正则表达式将是/a{3,5}h/
让A4 =“aaaah”;
让A2 =“aah”;
令multipleA = / a {3,5} h /;
multipleA.test(A4); //返回true
multipleA.test(A2); //返回false
+
+回想一下,使用加号+查找一个或多个字符,使用星号*查找零个或多个字符。这些都很方便,但有时需要匹配一定范围的匹配模式。 +可以使用数量说明符指定匹配模式的上下限。数量说明符与花括号({})一起使用。可以在花括号之间放两个数字,这两个数字代表匹配模式的上限和下限。 +例如,要在字符串"ah"中匹配仅出现35次的字母a,正则表达式应为/a{3,5}h/。 + +```js +let A4 = "aaaah"; +let A2 = "aah"; +let multipleA = /a{3,5}h/; +multipleA.test(A4); // Returns true +multipleA.test(A2); // Returns false +``` + +
## Instructions -
更改正则表达式ohRegex以匹配单词"Oh no"中的36字母h
+
+修改正则表达式ohRegex以匹配在"Oh no"中仅出现36次的字母h。 +
## Tests
```yml tests: - - text: 你的正则表达式应该使用大括号。 + - text: 你的正则表达式应该使用花括号。 testString: assert(ohRegex.source.match(/{.*?}/).length > 0); - - text: 你的正则表达式不应该匹配"Ohh no" + - text: "你的正则表达式不应该匹配'Ohh no'。" testString: assert(!ohRegex.test("Ohh no")); - - text: 你的正则表达式应该匹配"Ohhh no" + - text: "你的正则表达式应该匹配'Ohhh no'。" testString: assert("Ohhh no".match(ohRegex)[0].length === 7); - - text: 你的正则表达式应该匹配"Ohhhh no" + - text: 正则表达式应该匹配 "Ohhhh no"。 testString: assert("Ohhhh no".match(ohRegex)[0].length === 8); - - text: 你的正则表达式应该匹配"Ohhhhh no" + - text: "你的正则表达式应该匹配'Ohhhhh no'。" testString: assert("Ohhhhh no".match(ohRegex)[0].length === 9); - - text: 你的正则表达式应该匹配"Ohhhhhh no" + - text: "你的正则表达式应该匹配'Ohhhhhh no'。" testString: assert("Ohhhhhh no".match(ohRegex)[0].length === 10); - - text: 你的正则表达式不应该匹配"Ohhhhhhh no" + - text: "你的正则表达式不应该匹配'Ohhhhhhh no'。" testString: assert(!ohRegex.test("Ohhhhhhh no")); ``` @@ -38,26 +53,24 @@ tests: ## Challenge Seed
-
```js let ohStr = "Ohhh no"; let ohRegex = /change/; // Change this line let result = ohRegex.test(ohStr); - ```
- - -
## Solution
```js -// solution required +let ohStr = "Ohhh no"; +let ohRegex = /Oh{3,6} no/; // Change this line +let result = ohRegex.test(ohStr); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.chinese.md index 38ad41f3ae..a13f3b1b97 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/use-capture-groups-to-search-and-replace.chinese.md @@ -2,24 +2,44 @@ id: 587d7dbb367417b2b2512bab title: Use Capture Groups to Search and Replace challengeType: 1 -videoUrl: '' -localeTitle: 使用捕获组进行搜索和替换 +forumTopicId: 301368 +localeTitle: 使用捕获组搜索和替换 --- ## Description -
搜索很有用。但是,当它也更改(或替换)您匹配的文本时,您可以使搜索功能更强大。您可以在字符串上使用.replace()搜索和替换字符串中的文本。 .replace()的输入首先是您要搜索的正则表达式模式。第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。
let wrongText =“天空是银色的。”;
让silverRegex = / silver /;
wrongText.replace(silverRegex,“blue”);
//返回“天空是蓝色的”。
您还可以使用美元符号( $ )访问替换字符串中的捕获组。
“Code Camp”.replace(/(\ w +)\ s(\ w +)/,'$ 2 $ 1');
//返回“营地代码”
+
+搜索功能是很有用的。但是,当搜索同时也执行更改(或替换)匹配文本的操作时,搜索功能就会显得更加强大。 +可以使用字符串上.replace()方法来搜索并替换字符串中的文本。.replace()的输入首先是想要搜索的正则表达式匹配模式,第二个参数是用于替换匹配的字符串或用于执行某些操作的函数。 + +```js +let wrongText = "The sky is silver."; +let silverRegex = /silver/; +wrongText.replace(silverRegex, "blue"); +// Returns "The sky is blue." +``` + +你还可以使用美元符号($)访问替换字符串中的捕获组。 + +```js +"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1'); +// Returns "Camp Code" +``` + +
## Instructions -
写一个正则表达式,以便它搜索字符串"good" 。然后更新replaceText变量,将"good"替换为"okey-dokey"
+
+编写一个正则表达式,以搜索字符串"good"。然后更新变量replaceText,用字符串"okey-dokey"替换"good"。 +
## Tests
```yml tests: - - text: 您应该使用.replace()来搜索和替换。 + - text: 你应该使用.replace()搜索并替换。 testString: assert(code.match(/\.replace\(.*\)/)); - - text: 你的正则表达式应该改变"This sandwich is good." "This sandwich is okey-dokey." + - text: "你的正则表达式应该把'This sandwich is good.'变成'This sandwich is okey-dokey.'。" 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); - ``` @@ -51,6 +70,10 @@ let result = huhText.replace(fixRegex, replaceText);
```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); ``` +
diff --git a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/using-the-test-method.chinese.md b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/using-the-test-method.chinese.md index 39ec98f161..717255f88a 100644 --- a/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/using-the-test-method.chinese.md +++ b/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/regular-expressions/using-the-test-method.chinese.md @@ -2,24 +2,38 @@ id: 587d7db3367417b2b2512b8e title: Using the Test Method challengeType: 1 -videoUrl: '' +forumTopicId: 301369 localeTitle: 使用测试方法 --- ## Description -
正则表达式用于编程语言以匹配字符串的一部分。您可以创建模式来帮助您进行匹配。如果你想在字符串"The dog chased the cat"找到单词"the" ,你可以使用以下正则表达式: /the/ 。请注意,正则表达式中不需要引号。 JavaScript有多种方法可以使用正则表达式。测试正则表达式的一种方法是使用.test()方法。 .test()方法接受正则表达式,将其应用于字符串(放在括号内),如果模式发现或不存在,则返回truefalse
让testStr =“freeCodeCamp”;
让testRegex = / Code /;
testRegex.test(testStr);
//返回true
+
+在编程语言中,正则表达式用于匹配指定的字符串。通过正则表达式创建匹配模式(规则)可以帮你完成指定匹配。 +如果想要在字符串"The dog chased the cat"中匹配到"the"这个单词,可以使用如下正则表达式:/the/。注意,正则表达式中不需要引号。 +JavaScript 中有多种使用正则表达式的方法。测试正则表达式的一种方法是使用.test()方法。.test()方法会把编写的正则表达式和字符串(即括号内的内容)匹配,如果成功匹配到字符,则返回true,反之,返回false。 + +```js +let testStr = "freeCodeCamp"; +let testRegex = /Code/; +testRegex.test(testStr); +// Returns true +``` + +
## Instructions -
使用.test()方法在字符串myString上应用正则表达式myRegex
+
+使用.test()方法,检测字符串myString是否符合正则表达式myRegex定义的规则。 +
## Tests
```yml tests: - - text: 你应该使用.test()来测试正则表达式。 + - text: 你应该使用.test()方法来检测正则表达式。 testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/)); - - text: 您的结果应该返回true 。 + - text: 你的返回结果应该为true。 testString: assert(result === true); ``` @@ -35,7 +49,6 @@ tests: let myString = "Hello, World!"; let myRegex = /Hello/; let result = myRegex; // Change this line - ``` @@ -48,6 +61,9 @@ let result = myRegex; // Change this line
```js -// solution required +let myString = "Hello, World!"; +let myRegex = /Hello/; +let result = myRegex.test(myString); // Change this line ``` +