| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | id: 587d7db5367417b2b2512b96 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | title: 匹配字母表中的字母 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | challengeType: 1 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:14:01 +08:00
										 |  |  |  | forumTopicId: 301354 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | dashedName: match-letters-of-the-alphabet | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --description--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 了解了如何使用字符集(<dfn>character sets</dfn>)来指定要匹配的一组字符串,但是有时需要匹配大量字符(例如,字母表中的每个字母)。 有一种写法可以让实现这个功能变得简短。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 在字符集中,可以使用连字符(`-`)来定义要匹配的字符范围。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 例如,要匹配小写字母 `a` 到 `e`,你可以使用 `[a-e]`。 | 
					
						
							| 
									
										
										
										
											2020-08-04 15:14:01 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							|  |  |  |  | let catStr = "cat"; | 
					
						
							|  |  |  |  | let batStr = "bat"; | 
					
						
							|  |  |  |  | let matStr = "mat"; | 
					
						
							|  |  |  |  | let bgRegex = /[a-e]at/; | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | catStr.match(bgRegex); | 
					
						
							|  |  |  |  | batStr.match(bgRegex); | 
					
						
							|  |  |  |  | matStr.match(bgRegex); | 
					
						
							| 
									
										
										
										
											2020-08-04 15:14:01 +08:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 按顺序排列,三次 `match` 调用将返回值 `["cat"]`,`["bat"]` 和 `null`。 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 匹配字符串 `quoteSample` 中的所有字母。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | **注意**:一定要同时匹配大小写字母。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 你的正则表达式 `alphabetRegex` 应该匹配 35 项。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | assert(result.length == 35); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 你的正则表达式 `alphabetRegex` 应该使用全局标识。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert(alphabetRegex.flags.match(/g/).length == 1); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  |  | 你的正则表达式 `alphabetRegex` 应该使用忽略大小写标志。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | assert(alphabetRegex.flags.match(/i/).length == 1); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-04 15:14:01 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | # --seed--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							|  |  |  |  | let quoteSample = "The quick brown fox jumps over the lazy dog."; | 
					
						
							|  |  |  |  | let alphabetRegex = /change/; // Change this line | 
					
						
							|  |  |  |  | let result = alphabetRegex; // Change this line | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --solutions--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | let quoteSample = "The quick brown fox jumps over the lazy dog."; | 
					
						
							|  |  |  |  | let alphabetRegex = /[a-z]/gi; // Change this line | 
					
						
							|  |  |  |  | let result = quoteSample.match(alphabetRegex); // Change this line | 
					
						
							|  |  |  |  | ``` |