freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../regular-expressions/match-literal-strings.chine...

2.2 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7db3367417b2b2512b8f Match Literal Strings 1 匹配文字字符串

Description

在上一次挑战中,您使用正则表达式/Hello/搜索了单词"Hello" 。该正则表达式搜索字符串"Hello"的文字匹配。这是另一个搜索字符串"Kevin"的文字匹配的示例:
让testStr =“你好,我的名字是凯文。”;
让testRegex = / Kevin /;
testRegex.testtestStr;
//返回true
任何其他形式的"Kevin"都不匹配。例如,正则表达式/Kevin/将不匹配"kevin""KEVIN"
let wrongRegex = / kevin /;
wrongRegex.testtestStr;
//返回false
未来的挑战将展示如何匹配其他形式。

Instructions

完成正则表达式waldoRegex在字符串waldoIsHiding使用文字匹配查找"Waldo"

Tests

tests:
  - text: 你的正则表达式<code>waldoRegex</code>应该找到<code>&quot;Waldo&quot;</code>
    testString: 'assert(waldoRegex.test(waldoIsHiding), "Your regex <code>waldoRegex</code> should find <code>"Waldo"</code>");'
  - text: 你的正则表达式<code>waldoRegex</code>不应该搜索任何其他内容。
    testString: 'assert(!waldoRegex.test("Somewhere is hiding in this text."), "Your regex <code>waldoRegex</code> should not search for anything else.");'
  - text: 您应该与正则表达式执行文字字符串匹配。
    testString: 'assert(!/\/.*\/i/.test(code), "You should perform a literal string match with your regex.");'

Challenge Seed

let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
let waldoRegex = /search/; // Change this line
let result = waldoRegex.test(waldoIsHiding);

Solution

// solution required