2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 587d7db8367417b2b2512ba0
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 匹配除了字母和数字的所有符号
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-08-04 07:14:01 +00:00
|
|
|
forumTopicId: 301353
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: match-everything-but-letters-and-numbers
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
已经了解到可以使用缩写 `\w` 来匹配字母和数字 `[A-Za-z0-9_]`。 不过,有可能想要搜寻的匹配模式是非字母数字字符。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
可以使用 `\W` 搜寻和 `\w` 相反的匹配模式。 注意,相反匹配模式使用大写字母。 此缩写与 `[^A-Za-z0-9_]` 是一样的。
|
2020-08-04 07:14:01 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
let shortHand = /\W/;
|
|
|
|
let numbers = "42%";
|
|
|
|
let sentence = "Coding!";
|
2021-03-15 03:20:39 +00:00
|
|
|
numbers.match(shortHand);
|
|
|
|
sentence.match(shortHand);
|
2020-08-04 07:14:01 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
第一次 `match` 调用将返回值 `["%"]` 而第二次调用将返回 `["!"]`。
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
使用缩写 `\W` 来计算引号中所有非字符字母和数字字符的数量。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该使用全局标识。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(nonAlphabetRegex.global);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该在 `The five boxing wizards jump quickly.` 中匹配到 6 个非字母数字字符。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
'The five boxing wizards jump quickly.'.match(nonAlphabetRegex).length == 6
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
正则表达式应该使用元字符来匹配非字母字符。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(/\\W/.test(nonAlphabetRegex.source));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该在 `Pack my box with five dozen liquor jugs.` 中匹配到 8 个非字母数字字符。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
'Pack my box with five dozen liquor jugs.'.match(nonAlphabetRegex).length == 8
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该在 `How vexingly quick daft zebras jump!` 中匹配到 6 个非字母数字字符。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
'How vexingly quick daft zebras jump!'.match(nonAlphabetRegex).length == 6
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
你的正则表达式应该在字符串 `123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.` 中找到12个非字母数字字符。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(
|
|
|
|
'123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.'.match(nonAlphabetRegex)
|
|
|
|
.length == 12
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-08-04 07:14:01 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
let quoteSample = "The five boxing wizards jump quickly.";
|
|
|
|
let nonAlphabetRegex = /change/; // Change this line
|
|
|
|
let result = quoteSample.match(nonAlphabetRegex).length;
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
let quoteSample = "The five boxing wizards_jump quickly.";
|
|
|
|
let nonAlphabetRegex = /\W/g; // Change this line
|
|
|
|
let result = quoteSample.match(nonAlphabetRegex).length;
|
|
|
|
```
|