```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
-有时您不会(或不需要)知道模式中的确切字符。想到所有匹配的单词,比如拼写错误需要很长时间。幸运的是,您可以使用通配符来节省时间: .
通配符.
将匹配任何一个字符。通配符也称为dot
和period
。您可以像使用正则表达式中的任何其他字符一样使用通配符。例如,如果你想匹配"hug"
, "huh"
, "hut"
和"hum"
,你可以使用正则表达式/hu./
来匹配所有四个单词。 让humStr =“我会哼唱一首歌”;
让hugStr =“熊抱”;
让huRegex = /hu./;
humStr.match(huRegex); //返回[“hum”]
hugStr.match(huRegex); //返回[“拥抱”]
+
+有时不(或不需要)知道匹配模式中的确切字符。如果要精确匹配到完整的单词,那出现一个拼写错误就会匹配不到。幸运的是,可以使用通配符.
来处理这种情况。
+通配符.
将匹配任何一个字符。通配符也叫dot
或period
。可以像使用正则表达式中任何其他字符一样使用通配符。例如,如果想匹配"hug"
、"huh"
、"hut"
和"hum"
,可以使用正则表达式/hu./
匹配以上四个单词。
+
+```js
+let humStr = "I'll hum a song";
+let hugStr = "Bear hug";
+let huRegex = /hu./;
+huRegex.test(humStr); // Returns true
+huRegex.test(hugStr); // Returns true
+```
+
+
## 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 set
, caret
用于在字符串的开头搜索模式。 让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
-最后一项挑战使用加+
号来查找出现一次或多次的字符。还有一个选项可以匹配出现零次或多次的字符。执行此操作的字符是asterisk
或star
asterisk
: *
。 让soccerWord =“gooooooooal!”;
让gPhrase =“直觉”;
让oPhrase =“越过月亮”;
let goRegex = / go * /;
soccerWord.match(goRegex); //返回[“goooooooo”]
gPhrase.match(goRegex); //返回[“g”]
oPhrase.match(goRegex); //返回null
+
+上一次的挑战中使用了加号+
来查找出现一次或多次的字符。还有一个选项可以匹配出现零次或多次的字符。
+执行该操作的字符叫做asterisk
或star
,即*
。
+
+```js
+let soccerWord = "gooooooooal!";
+let gPhrase = "gut feeling";
+let oPhrase = "over the moon";
+let goRegex = /go*/;
+soccerWord.match(goRegex); // Returns ["goooooooo"]
+gPhrase.match(goRegex); // Returns ["g"]
+oPhrase.match(goRegex); // Returns null
+```
+
+
## 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
范围: -
。例如,要匹配小写字母a
到e
您将使用[ae]
。 让catStr =“猫”;
让batStr =“蝙蝠”;
让matStr =“mat”;
让bgRegex = / [ae] at /;
catStr.match(bgRegex); //返回[“cat”]
batStr.match(bgRegex); //返回[“bat”]
matStr.match(bgRegex); //返回null
+
+了解了如何使用字符集
来指定要匹配的一组字符串,但是当需要匹配大量字符(例如,字母表中的每个字母)时,有一种写法可以让实现这个功能变得简短。
+在字符集
中,可以使用连字符
(-
)来定义要匹配的字符范围。
+例如,要匹配小写字母a
到e
,你可以使用[a-e]
。
+
+```js
+let catStr = "cat";
+let batStr = "bat";
+let matStr = "mat";
+let bgRegex = /[a-e]at/;
+catStr.match(bgRegex); // Returns ["cat"]
+batStr.match(bgRegex); // Returns ["bat"]
+matStr.match(bgRegex); // Returns null
+```
+
+
## 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]/
匹配0
到5
之间的任何数字,包括0
和5
。此外,可以在单个字符集中组合一系列字母和数字。 让jennyStr =“Jenny8675309”;
让myRegex = / [a-z0-9] / ig;
//匹配jennyStr中的所有字母和数字
jennyStr.match(myRegex);
+
+使用连字符(-
)匹配字符范围并不仅限于字母。它还可以匹配一系列数字。
+例如,/[0-5]/
匹配0
和5
之间的任意数字,包含0
和5
。
+此外,还可以在单个字符集中组合一系列字母和数字。
+
+```js
+let jennyStr = "Jenny8675309";
+let myRegex = /[a-z0-9]/ig;
+// matches all letters and numbers in jennyStr
+jennyStr.match(myRegex);
+```
+
+
## Instructions
-创建一个与h
和s
之间的字母范围匹配的正则表达式,以及介于2
和6
之间的数字范围。请记住在正则表达式中包含适当的标志。
+
+创建一个正则表达式,使其可以匹配h
和s
之间的一系列字母,以及2
和6
之间的一系列数字。请记得在正则表达式中包含恰当的标志。
+
## 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
使用带元音( a
, e
, i
, o
, u
)的字符类来查找字符串quoteSample
中的所有元音。 注意
确保匹配大写和小写元音。
+
+使用元音字符集(a
、e
、i
、o
、u
)在正则表达式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在字符串中向前看以进一步检查模式。当您想要在同一个字符串上搜索多个模式时,这非常有用。有两种lookaheads
: positive lookahead
和negative 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
-使用lookaheads
在pwRegex
匹配长的时间大于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"
出现3
到5
次的字母a
,你的正则表达式将是/a{3,5}h/
。 让A4 =“aaaah”;
让A2 =“aah”;
令multipleA = / a {3,5} h /;
multipleA.test(A4); //返回true
multipleA.test(A2); //返回false
+
+回想一下,使用加号+
查找一个或多个字符,使用星号*
查找零个或多个字符。这些都很方便,但有时需要匹配一定范围的匹配模式。
+可以使用数量说明符
指定匹配模式的上下限。数量说明符与花括号({
和}
)一起使用。可以在花括号之间放两个数字,这两个数字代表匹配模式的上限和下限。
+例如,要在字符串"ah"
中匹配仅出现3
到5
次的字母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"
中的3
到6
字母h
。
+
+修改正则表达式ohRegex
以匹配在"Oh no"
中仅出现3
到6
次的字母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()
方法接受正则表达式,将其应用于字符串(放在括号内),如果模式发现或不存在,则返回true
或false
。 让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
```
+