freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../regular-expressions/match-characters-that-occur...

2.4 KiB

id title challengeType videoUrl localeTitle
587d7db6367417b2b2512b99 Match Characters that Occur One or More Times 1 匹配出现一次或多次的字符

Description

有时,您需要匹配连续出现一次或多次的字符(或字符组)。这意味着它至少发生一次,并且可以重复。您可以使用+字符来检查是否是这种情况。请记住,角色或模式必须连续出现。也就是说,角色必须一个接一个地重复。例如, /a+/g会在"abc"找到一个匹配并返回["a"] 。由于+ ,它也会在"aabc"找到一个匹配并返回["aa"] 。如果它是检查字符串"abab" ,它会找到两个匹配并返回["a", "a"]因为a字符不在一行 - 它们之间有一个b 。最后,由于字符串"bcd"没有"a" "bcd" ,因此找不到匹配项。

Instructions

您希望在"Mississippi"字母s出现一次或多次时找到匹配项。写一个使用+符号的正则表达式。

Tests

tests:
  - text: 你的正则表达式<code>myRegex</code>应该使用<code>+</code>符号来匹配一个或多个<code>s</code>字符。
    testString: 'assert(/\+/.test(myRegex.source), "Your regex <code>myRegex</code> should use the <code>+</code> sign to match one or more <code>s</code> characters.");'
  - text: 你的正则表达式<code>myRegex</code>应该匹配2个项目。
    testString: 'assert(result.length == 2, "Your regex <code>myRegex</code> should match 2 items.");'
  - text: <code>result</code>变量应该是一个包含两个匹配<code>&quot;ss&quot;</code>的数组
    testString: 'assert(result[0] == "ss" && result[1] == "ss", "The <code>result</code> variable should be an array with two matches of <code>"ss"</code>");'

Challenge Seed

let difficultSpelling = "Mississippi";
let myRegex = /change/; // Change this line
let result = difficultSpelling.match(myRegex);

Solution

// solution required