freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../regular-expressions/match-numbers-and-letters-o.../index.md

1.4 KiB
Raw Blame History

title localeTitle
Match Numbers and Letters of the Alphabet 匹配数字和字母的字母

匹配数字和字母的字母

在此挑战中系统会要求您返回从字符串中提取的数字和字母的集合。我们的目标是创建一个单一的正则表达式捕获h和s之间的字母范围以及2到6之间的数字。

提示1

你在使用match方法吗如果是这样那么你是否从适当的变量调用方法

  let input_string = "The string you are testing on" 
  let yourRegExp = /[hs]/; 
  let correct_result = input_string.match(yourRegExp); // passes - returns characters H to S 
 
  let incorrect_result = yourRegExp.match(input_string); // fails - .match() is not a function 

提示2

您是否记得启用正则表达式标志例如“i”表示忽略大小写而“g”表示是否可以检索多个值如果是这样那么你是否包括数字和字母的字符大小写匹配

let regexp = /[a-z1-100]/ig 
 // above code returns all characters from A to Z, along with all numbers from 1 to 100 
 // this includes the letter A and Z and the numbers 1 and 100 

扰流警报 - 提前解决

let quoteSample = "Blueberry 3.141592653s are delicious."; 
 let myRegex = /[h-s2-6]/ig; // Change this line 
 let result = quoteSample.match(myRegex); // Change this line