2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7db3367417b2b2512b8f
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
forumTopicId: 301355
|
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>/Hello/</code>搜索到了字符串<code>"Hello"</code>。那个正则表达式在字符串中搜寻<code>"Hello"</code>的文字匹配。下面是另一个在字符串中搜寻<code>"Kevin"</code>的示例:
|
|
|
|
|
|
|
|
```js
|
|
|
|
let testStr = "Hello, my name is Kevin.";
|
|
|
|
let testRegex = /Kevin/;
|
|
|
|
testRegex.test(testStr);
|
|
|
|
// Returns true
|
|
|
|
```
|
|
|
|
|
|
|
|
任何其他形式的<code>"Kevin"</code>都不会被匹配。例如,正则表达式<code>/Kevin/</code>不会匹配<code>"kevin"</code>或者<code>"KEVIN"</code>。
|
|
|
|
|
|
|
|
```js
|
|
|
|
let wrongRegex = /kevin/;
|
|
|
|
wrongRegex.test(testStr);
|
|
|
|
// Returns false
|
|
|
|
```
|
|
|
|
|
|
|
|
后续的挑战将为你展示如何匹配其他形式的字符串。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
完成正则表达式<code>waldoRegex</code>,在字符串<code>waldoIsHiding</code>中匹配到文本<code>"Waldo"</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>waldoRegex</code>应该匹配到<code>'Waldo'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(waldoRegex.test(waldoIsHiding));
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: 你的正则表达式<code>waldoRegex</code>不应该搜寻其他的任何内容。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(!waldoRegex.test('Somewhere is hiding in this text.'));
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: 你应该使用你的正则表达式对字符串执行文字匹配。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(!/\/.*\/i/.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
|
|
|
|
let waldoRegex = /search/; // Change this line
|
|
|
|
let result = waldoRegex.test(waldoIsHiding);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
|
|
|
|
let waldoRegex = /Waldo/; // Change this line
|
|
|
|
let result = waldoRegex.test(waldoIsHiding);
|
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>
|