2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7db3367417b2b2512b8e
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
forumTopicId: 301369
|
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>"The dog chased the cat"</code>中匹配到<code>"the"</code>这个单词,可以使用如下正则表达式:<code>/the/</code>。注意,正则表达式中不需要引号。
|
|
|
|
JavaScript 中有多种使用正则表达式的方法。测试正则表达式的一种方法是使用<code>.test()</code>方法。<code>.test()</code>方法会把编写的正则表达式和字符串(即括号内的内容)匹配,如果成功匹配到字符,则返回<code>true</code>,反之,返回<code>false</code>。
|
|
|
|
|
|
|
|
```js
|
|
|
|
let testStr = "freeCodeCamp";
|
|
|
|
let testRegex = /Code/;
|
|
|
|
testRegex.test(testStr);
|
|
|
|
// Returns true
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
使用<code>.test()</code>方法,检测字符串<code>myString</code>是否符合正则表达式<code>myRegex</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>.test()</code>方法来检测正则表达式。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/myRegex.test\(\s*myString\s*\)/));
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: 你的返回结果应该为<code>true</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(result === true);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
let myString = "Hello, World!";
|
|
|
|
let myRegex = /Hello/;
|
|
|
|
let result = myRegex; // Change this line
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
let myString = "Hello, World!";
|
|
|
|
let myRegex = /Hello/;
|
|
|
|
let result = myRegex.test(myString); // Change this line
|
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>
|