ZhichengChen 2fdc5267e3
fix(i18n): update Chinese translation of regular expressions (#38042)
Co-authored-by: Zhicheng Chen <chenzhicheng@dayuwuxian.com>
2020-08-04 12:44:01 +05:30

1.9 KiB

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7db4367417b2b2512b93 Find More Than the First Match 1 301342 全局匹配

Description

到目前为止,只能提取或搜寻一次模式匹配。
let testStr = "Repeat, Repeat, Repeat";
let ourRegex = /Repeat/;
testStr.match(ourRegex);
// Returns ["Repeat"]

若要多次搜寻或提取模式匹配,可以使用g标志。

let repeatRegex = /Repeat/g;
testStr.match(repeatRegex);
// Returns ["Repeat", "Repeat", "Repeat"]

Instructions

使用正则表达式starRegex,从字符串twinkleStar中匹配到所有的"Twinkle"单词并提取出来。 注意:
在正则表达式上可以有多个标志,比如/search/gi

Tests

tests:
  - text: 你的正则表达式<code>starRegex</code>应该使用全局标志<code>g</code>。
    testString: assert(starRegex.flags.match(/g/).length == 1);
  - text: 你的正则表达式<code>starRegex</code>应该使用忽略大小写标志<code>i</code>。
    testString: assert(starRegex.flags.match(/i/).length == 1);
  - text: "你的匹配应该匹配单词<code>'Twinkle'</code>的两个匹配项。"
    testString: assert(result.sort().join() == twinkleStar.match(/twinkle/gi).sort().join());
  - text: 你的匹配<code>结果</code>应该包含两个元素。
    testString: assert(result.length == 2);

Challenge Seed

let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /change/; // Change this line
let result = twinkleStar; // Change this line

Solution

let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /twinkle/gi;
let result = twinkleStar.match(starRegex);