2020-10-06 23:10:08 +05:30

3.6 KiB

id, challengeType, forumTopicId, localeTitle
id challengeType forumTopicId localeTitle
587d7db5367417b2b2512b94 1 301348 用通配符.匹配任何内容

Description

有时不(或不需要)知道匹配模式中的确切字符。如果要精确匹配到完整的单词,那出现一个拼写错误就会匹配不到。幸运的是,可以使用通配符.来处理这种情况。 通配符.将匹配任何一个字符。通配符也叫dotperiod。可以像使用正则表达式中任何其他字符一样使用通配符。例如,如果想匹配"hug""huh""hut""hum",可以使用正则表达式/hu./匹配以上四个单词。
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"。正则表达式中应该使用通配符。

Tests

tests:
  - text: 你应该使用<code>.test()</code>方法。
    testString: assert(code.match(/\.test\(.*\)/));
  - text: 你应该在你的正则表达式<code>unRegex</code>中使用通配符。
    testString: assert(/\./.test(unRegex.source));
  - text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'Let us go on a run.'</code>中匹配到<code>'run'</code>单词。"
    testString: assert(unRegex.test("Let us go on a run."));
  - text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'The sun is out today.'</code>中匹配到<code>'sun'</code>单词。"
    testString: assert(unRegex.test("The sun is out today."));
  - text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'Coding is a lot of fun.'</code>中匹配到<code>'fun'</code>单词。"
    testString: assert(unRegex.test("Coding is a lot of fun."));
  - text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'Seven days without a pun makes one weak.'</code>中匹配到<code>'pun'</code>单词。"
    testString: assert(unRegex.test("Seven days without a pun makes one weak."));
  - text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'One takes a vow to be a nun.'</code>中匹配到<code>'nun'</code>单词。"
    testString: assert(unRegex.test("One takes a vow to be a nun."));
  - text: "你的正则表达式<code>unRegex</code>应该在字符串<code>'She got fired from the hot dog stand for putting her hair in a bun.'</code>中匹配到<code>'bun'</code>单词。"
    testString: assert(unRegex.test("She got fired from the hot dog stand for putting her hair in a bun."));
  - text: "你的正则表达式<code>unRegex</code>不应该匹配<code>'There is a bug in my code.'</code>。"
    testString: assert(!unRegex.test("There is a bug in my code."));
  - text: "你的正则表达式<code>unRegex</code>不应该匹配<code>'Catch me if you can.'</code>。"
    testString: assert(!unRegex.test("Can me if you can."));

Challenge Seed

let exampleStr = "Let's have fun with regular expressions!";
let unRegex = /change/; // Change this line
let result = unRegex.test(exampleStr);

Solution

let exampleStr = "Let's have fun with regular expressions!";
let unRegex = /.un/; // Change this line
let result = unRegex.test(exampleStr);