freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../regular-expressions/match-ending-string-pattern...

2.0 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7db7367417b2b2512b9e Match Ending String Patterns 1 匹配结束字符串模式

Description

在上一个挑战中,您学会了使用caret来搜索字符串开头的模式。还有一种方法可以在字符串末尾搜索模式。您可以使用正则表达式末尾的dollar sign字符$来搜索字符串的结尾。
让theEnding =“这是一个永无止境的故事”;
让storyRegex = / story $ /;
storyRegex.testtheEnding;
//返回true
让noEnding =“有时故事必须结束”;
storyRegex.testnoEnding;
//返回false

Instructions

使用锚字符( $ )来匹配字符串"caboose"在字符串的结尾caboose

Tests

tests:
  - text: 您应该在正则表达式中使用美元符号<code>$</code> anchor搜索<code>&quot;caboose&quot;</code> 。
    testString: 'assert(lastRegex.source == "caboose$", "You should search for <code>"caboose"</code> with the dollar sign <code>$</code> anchor in your regex.");'
  - text: 你的正则表达式不应该使用任何标志。
    testString: 'assert(lastRegex.flags == "", "Your regex should not use any flags.");'
  - text: 您应该在字符串末尾匹配<code>&quot;caboose&quot;</code> <code>&quot;The last car on a train is the caboose&quot;</code>
    testString: 'assert(lastRegex.test("The last car on a train is the caboose"), "You should match <code>"caboose"</code> at the end of the string <code>"The last car on a train is the caboose"</code>");'

Challenge Seed

let caboose = "The last car on a train is the caboose";
let lastRegex = /change/; // Change this line
let result = lastRegex.test(caboose);

Solution

// solution required