caret
来搜索字符串开头的模式。还有一种方法可以在字符串末尾搜索模式。您可以使用正则表达式末尾的dollar sign
字符$
来搜索字符串的结尾。 让theEnding =“这是一个永无止境的故事”;
让storyRegex = / story $ /;
storyRegex.test(theEnding);
//返回true
让noEnding =“有时故事必须结束”;
storyRegex.test(noEnding);
//返回false
$
)来匹配字符串"caboose"
在字符串的结尾caboose
。 $
anchor搜索"caboose"
。
testString: assert(lastRegex.source == "caboose$");
- text: 你的正则表达式不应该使用任何标志。
testString: assert(lastRegex.flags == "");
- text: 您应该在字符串末尾匹配"caboose"
"The last car on a train is the caboose"
testString: assert(lastRegex.test("The last car on a train is the caboose"));
```