3.7 KiB
3.7 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7dbb367417b2b2512baa | Reuse Patterns Using Capture Groups | 1 | 使用捕获组重用模式 |
Description
capture groups
搜索重复子字符串。括号(
和)
用于查找重复子串。你把模式的正则表达式重复在括号之间。要指定重复字符串的显示位置,请使用反斜杠( \
),然后使用数字。此数字从1开始,随着您使用的每个其他捕获组而增加。一个例子是\1
来匹配第一组。下面的示例匹配以空格分隔的两次出现的任何单词: 让repeatStr =“正则表达式正则表达式”;对字符串使用
let repeatRegex = /(\ w +)\ s \ 1 /;
repeatRegex.test(repeatStr); //返回true
repeatStr.match(repeatRegex); //返回[“regex regex”,“regex”]
.match()
方法将返回一个数组,其中包含与其匹配的字符串及其捕获组。 Instructions
reRegex
使用capture groups
来匹配在字符串中仅重复三次的数字,每个数字用空格分隔。 Tests
tests:
- text: 你的正则表达式应该使用数字的速记字符类。
testString: 'assert(reRegex.source.match(/\\d/), "Your regex should use the shorthand character class for digits.");'
- text: 您的正则表达式应该重复使用捕获组两次。
testString: 'assert(reRegex.source.match(/\\\d/g).length === 2, "Your regex should reuse the capture group twice.");'
- text: 你的正则表达式应该有两个空格来分隔这三个数字。
testString: 'assert(reRegex.source.match(/\\s/g).length === 2, "Your regex should have two spaces separating the three numbers.");'
- text: 你的正则表达式应该匹配<code>"42 42 42"</code> 。
testString: 'assert(reRegex.test("42 42 42"), "Your regex should match <code>"42 42 42"</code>.");'
- text: 你的正则表达式应该匹配<code>"100 100 100"</code> 。
testString: 'assert(reRegex.test("100 100 100"), "Your regex should match <code>"100 100 100"</code>.");'
- text: 你的正则表达式不应该匹配<code>"42 42 42 42"</code> 。
testString: 'assert.equal(("42 42 42 42").match(reRegex.source), null, "Your regex should not match <code>"42 42 42 42"</code>.");'
- text: 你的正则表达式不应该匹配<code>"42 42"</code> 。
testString: 'assert.equal(("42 42").match(reRegex.source), null, "Your regex should not match <code>"42 42"</code>.");'
- text: 你的正则表达式不应该匹配<code>"101 102 103"</code> 。
testString: 'assert(!reRegex.test("101 102 103"), "Your regex should not match <code>"101 102 103"</code>.");'
- text: 你的正则表达式不应该匹配<code>"1 2 3"</code> 。
testString: 'assert(!reRegex.test("1 2 3"), "Your regex should not match <code>"1 2 3"</code>.");'
- text: 你的正则表达式应匹配<code>"10 10 10"</code> 。
testString: 'assert(reRegex.test("10 10 10"), "Your regex should match <code>"10 10 10"</code>.");'
Challenge Seed
let repeatNum = "42 42 42";
let reRegex = /change/; // Change this line
let result = reRegex.test(repeatNum);
Solution
// solution required