Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.7 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dba367417b2b2512ba9 正向先行断言和负向先行断言 1 301360 positive-and-negative-lookahead

--description--

先行断言是告诉 JavaScript 在字符串中向前查找的匹配模式。当想要在同一个字符串上搜寻多个匹配模式时,这可能非常有用。

有两种先行断言正向先行断言负向先行断言

正向先行断言会查看并确保搜索匹配模式中的元素存在,但实际上并不匹配。正向先行断言的用法是(?=...),其中...就是需要存在但不会被匹配的部分。

另一方面,负向先行断言会查看并确保搜索匹配模式中的元素不存在。负向先行断言的用法是(?!...),其中...是希望不存在的匹配模式。如果负向先行断言部分不存在,将返回匹配模式的其余部分。

尽管先行断言有点儿令人困惑,但是这些示例会有所帮助。

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 个字符且至少包含一个数字:

let password = "abc123";
let checkPass = /(?=\w{3,6})(?=\D*\d)/;
checkPass.test(password); // Returns true

--instructions--

在正则表达式pwRegex中使用先行断言以匹配大于5个字符且有两个连续数字的密码并且不能以数字开头。

--hints--

你的正则表达式应该使用两个正向先行断言

assert(pwRegex.source.match(/\(\?=.*?\)\(\?=.*?\)/) !== null);

你的正则表达式不应该匹配'astronaut'

assert(!pwRegex.test('astronaut'));

你的正则表达式不应该匹配'airplanes'

assert(!pwRegex.test('airplanes'));

正则不应该匹配 "banan1"

assert(!pwRegex.test('banan1'));

你的正则表达式应该匹配'bana12'

assert(pwRegex.test('bana12'));

你的正则表达式应该匹配'abc123'

assert(pwRegex.test('abc123'));

你的正则表达式不应该匹配'123'

assert(!pwRegex.test('123'));

你的正则表达式不应该匹配'1234'

assert(!pwRegex.test('1234'));

正则不应该匹配 "8pass99"

assert(!pwRegex.test('8pass99'));

正则不应该匹配 "12abcde"

assert(!pwRegex.test('12abcde'));

--seed--

--seed-contents--

let sampleWord = "astronaut";
let pwRegex = /change/; // Change this line
let result = pwRegex.test(sampleWord);

--solutions--

var pwRegex =  /^\D(?=\w{5})(?=\w*\d{2})/;