2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7db7367417b2b2512b9e
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
forumTopicId: 301352
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 匹配字符串的末尾
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='description'>
|
|
|
|
在上一个挑战中,学习了使用<code>^</code>符号来搜寻字符串开头的匹配模式。还有一种方法可以搜寻字符串末尾的匹配模式。
|
|
|
|
可以使用正则表达式的<code>美元</code>符号<code>$</code>来搜寻字符串的结尾。
|
|
|
|
|
|
|
|
```js
|
|
|
|
let theEnding = "This is a never ending story";
|
|
|
|
let storyRegex = /story$/;
|
|
|
|
storyRegex.test(theEnding);
|
|
|
|
// Returns true
|
|
|
|
let noEnding = "Sometimes a story will have to end";
|
|
|
|
storyRegex.test(noEnding);
|
|
|
|
// Returns false
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
使用<code>$</code>在字符串<code>caboose</code>的末尾匹配<code>"caboose"</code>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你应该在正则表达式使用美元符号<code>$</code>来搜寻<code>'caboose'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(lastRegex.source == "caboose$");
|
2018-10-10 18:03:03 -04:00
|
|
|
- text: 你的正则表达式不应该使用任何标志。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(lastRegex.flags == "");
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你应该在字符串<code>'The last car on a train is the caboose'</code>的末尾匹配<code>'caboose'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(lastRegex.test("The last car on a train is the caboose"));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
let caboose = "The last car on a train is the caboose";
|
|
|
|
let lastRegex = /change/; // Change this line
|
|
|
|
let result = lastRegex.test(caboose);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
let caboose = "The last car on a train is the caboose";
|
|
|
|
let lastRegex = /caboose$/; // Change this line
|
|
|
|
let result = lastRegex.test(caboose);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:14:01 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|