在上一个挑战中,学习了使用^
符号来搜寻字符串开头的匹配模式。
还有一种方法可以搜寻字符串末尾的匹配模式。
可以使用正则表达式的美元
符号$
来搜寻字符串的结尾。
var theEnding = "This is a never ending story";
var storyRegex = /story$/;
console.log( storyRegex.test(theEnding) ); // 返回 true
var noEnding = "Sometimes a story will have to end";
console.log( storyRegex.test(noEnding) ); // 返回 false
闯关:使用$
在字符串caboose
的末尾匹配"caboose"
。
var caboose = "The last car on a train is the caboose";
var lastRegex = /change/; // 修改这行
var result = lastRegex.test(caboose);
console.log(result);
正确代码
var caboose = "The last car on a train is the caboose";
var lastRegex = /caboose$/;
var result = lastRegex.test(caboose);
console.log(result);