2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7db8367417b2b2512ba0
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
forumTopicId: 301353
|
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>\w</code>来匹配字母和数字<code>[A-Za-z0-9_]</code>。不过,有可能想要搜寻的匹配模式是非字母数字字符。
|
|
|
|
可以使用<code>\W</code>搜寻和<code>\w</code>相反的匹配模式。注意,相反匹配模式使用大写字母。此缩写与<code>[^A-Za-z0-9_]</code>是一样的。
|
|
|
|
|
|
|
|
```js
|
|
|
|
let shortHand = /\W/;
|
|
|
|
let numbers = "42%";
|
|
|
|
let sentence = "Coding!";
|
|
|
|
numbers.match(shortHand); // Returns ["%"]
|
|
|
|
sentence.match(shortHand); // Returns ["!"]
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
使用缩写<code>\W</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: 你的正则表达式应该使用全局状态修正符。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(nonAlphabetRegex.global);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式应该在<code>'The five boxing wizards jump quickly.'</code>中匹配到 6 个非字母数字字符。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert("The five boxing wizards jump quickly.".match(nonAlphabetRegex).length == 6);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: 正则表达式应该使用元字符来匹配非字母字符。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/\\W/.test(nonAlphabetRegex.source));
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式应该在<code>'Pack my box with five dozen liquor jugs.'</code>中匹配到 8 个非字母数字字符。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert("Pack my box with five dozen liquor jugs.".match(nonAlphabetRegex).length == 8);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式应该在<code>'How vexingly quick daft zebras jump!'</code>中匹配到 6 个非字母数字字符。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert("How vexingly quick daft zebras jump!".match(nonAlphabetRegex).length == 6);
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "你的正则表达式应该在<code>'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'</code>中匹配到 12 个非字母数字字符。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert("123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.".match(nonAlphabetRegex).length == 12);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
let quoteSample = "The five boxing wizards jump quickly.";
|
|
|
|
let nonAlphabetRegex = /change/; // Change this line
|
|
|
|
let result = quoteSample.match(nonAlphabetRegex).length;
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-08-04 15:14:01 +08:00
|
|
|
let quoteSample = "The five boxing wizards_jump quickly.";
|
|
|
|
let nonAlphabetRegex = /\W/g; // Change this line
|
|
|
|
let result = quoteSample.match(nonAlphabetRegex).length;
|
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>
|