freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../regular-expressions/match-all-letters-and-numbe...

3.6 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7db7367417b2b2512b9f Match All Letters and Numbers 1 匹配所有字母和数字

Description

使用字符类,您可以使用[az]搜索字母表中的所有字母。这种字符类很常见,它有一个快捷方式,虽然它还包含一些额外的字符。 JavaScript中与字母表匹配的最接近的字符类是\w 。此快捷方式等于[A-Za-z0-9_] 。此字符类匹配大写和小写字母加数字。注意,此字符类还包括下划线字符( _ )。
让longHand = / [A-Za-z0-9 _] + /;
让shortHand = / \ w + /;
让数字=“42”;
let varNames =“important_var”;
longHand.test数字; //返回true
shortHand.test数字; //返回true
longHand.testvarNames; //返回true
shortHand.testvarNames; //返回true
这些快捷方式字符类也称为shorthand character classes

Instructions

使用速记字符类\w来计算各种引号和字符串中的字母数字字符数。

Tests

tests:
  - text: 你的正则表达式应该使用全局标志。
    testString: 'assert(alphabetRegexV2.global, "Your regex should use the global flag.");'
  - text: 你的正则表达式应该使用速记字符
    testString: 'assert(/\\w/.test(alphabetRegexV2.source), "Your regex should use the shorthand character <code>\w</code> to match all characters which are alphanumeric.");'
  - text: 你的正则表达式应该在<code>&quot;The five boxing wizards jump quickly.&quot;</code>找到31个字母数字字符<code>&quot;The five boxing wizards jump quickly.&quot;</code>
    testString: 'assert("The five boxing wizards jump quickly.".match(alphabetRegexV2).length === 31, "Your regex should find 31 alphanumeric characters in <code>"The five boxing wizards jump quickly."</code>");'
  - text: 你的正则表达式应该在<code>&quot;Pack my box with five dozen liquor jugs.&quot;</code>找到32个字母数字字符<code>&quot;Pack my box with five dozen liquor jugs.&quot;</code>
    testString: 'assert("Pack my box with five dozen liquor jugs.".match(alphabetRegexV2).length === 32, "Your regex should find 32 alphanumeric characters in <code>"Pack my box with five dozen liquor jugs."</code>");'
  - text: 你的正则表达式应该在<code>&quot;How vexingly quick daft zebras jump!&quot;</code>找到30个字母数字字符<code>&quot;How vexingly quick daft zebras jump!&quot;</code>
    testString: 'assert("How vexingly quick daft zebras jump!".match(alphabetRegexV2).length === 30, "Your regex should find 30 alphanumeric characters in <code>"How vexingly quick daft zebras jump!"</code>");'
  - text: 你的正则表达式应该在<code>&quot;123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.&quot;</code>找到36个字母数字字符<code>&quot;123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.&quot;</code>
    testString: 'assert("123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ.".match(alphabetRegexV2).length === 36, "Your regex should find 36 alphanumeric characters in <code>"123 456 7890 ABC def GHI jkl MNO pqr STU vwx YZ."</code>");'

Challenge Seed

let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /change/; // Change this line
let result = quoteSample.match(alphabetRegexV2).length;

Solution

// solution required