2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7db7367417b2b2512b9d
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
forumTopicId: 301349
|
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>)来创建一个<code>否定字符集</code>,形如<code>[^thingsThatWillNotBeMatched]</code>。在<code>字符集</code>之外,<code>插入</code>符号用于字符串的开头搜寻匹配模式。
|
|
|
|
|
|
|
|
```js
|
|
|
|
let firstString = "Ricky is first and can be found.";
|
|
|
|
let firstRegex = /^Ricky/;
|
|
|
|
firstRegex.test(firstString);
|
|
|
|
// Returns true
|
|
|
|
let notFirst = "You can't find Ricky now.";
|
|
|
|
firstRegex.test(notFirst);
|
|
|
|
// 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>rickyAndCal</code>的开头出现的<code>"Cal"</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>'Cal'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(calRegex.source == "^Cal");
|
2018-10-10 18:03:03 -04:00
|
|
|
- text: 你的正则表达式不应该使用任何标志。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(calRegex.flags == "");
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式应该匹配字符串开头的<code>'Cal'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(calRegex.test("Cal and Ricky both like racing."));
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式不应该匹配字符串中间的<code>'Cal'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(!calRegex.test("Ricky and Cal both like racing."));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
let rickyAndCal = "Cal and Ricky both like racing.";
|
|
|
|
let calRegex = /change/; // Change this line
|
|
|
|
let result = calRegex.test(rickyAndCal);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
let rickyAndCal = "Cal and Ricky both like racing.";
|
|
|
|
let calRegex = /^Cal/; // Change this line
|
|
|
|
let result = calRegex.test(rickyAndCal);
|
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>
|