当前位置: 首页 > 科技观察

JS正则表达式备忘单

时间:2023-03-12 08:15:48 科技观察

正则表达式或“正则表达式”用于匹配部分字符串下面是我创建正则表达式的备忘单。匹配正则使用.test()方法lettestString="Myteststring";lettestRegex=/string/;testRegex.test(testString);匹配多个模式使用操作符号|constregex=/yes|no|maybe/;ignorecaseuseiFlag表示忽略大小写constcaseInsensitiveRegex=/ignorecase/i;consttestString='WeusetheiflagtoiGnOrECasE';caseInsensitiveRegex.test(testString);//true提取变量的第一个匹配使用.match()方法constmatch="HelloWorld!".match(/hello/i);//"Hello"使用g标志提取数组中的所有匹配项"repeat","re??PeAT"]使用通配符匹配任何字符。作为任何字符的占位符//Tomatch"cat","BAT","fAT","mat"constregexWithWildcard=/.at/gi;consttestString="catBATcupcakefATmatdog";installMatchingWords=testString.match(regexWithWildcard);//["cat","BAT","fAT","mat"]以多种可能性匹配单个字符使用字符类,可以使用它来定义匹配一组字符放在方括号中[]//匹配"cat&》;“fat”和“mat”但不是“bat”"]使用字符集内的范围匹配字母表中的字母[a-z]constregexWidthCharRange=/[a-e]at/;constregexWithCharRange=/[a-e]at/;constcatString="cat";constbatString="bat";constfatString="fat";regexWithCharRange.test(catString);//trueregexWithCharRange.test(batString);//trueregexWithCharRange.test(fatString);//false匹配特定的数字和字母也可以使用连字符来匹配数字constregexWithLetterAndNumberRange=/[a-z0-9]/ig;consttestString="Emma19382";testString.match(regexWithLetterAndNumberRange)//true匹配单个未知字符要匹配一组你不想拥有的字符,使用否定字符集^constallCharsNotVowels=/[^aeiou]/gi;constallCharsNotVowelsOrNumbers=/[^aeiou0-9]/gi;匹配在一行中出现一次或多次的字符,使用+标志constoneOrMoreAsRegex=/a+/gi;constoneOrMoreSsRegex=/s+/gi;constcityInFlorida="Tallahassee";cityInFlorida.match(oneOrMoreAsRegex);//['a','a','a'];cityInFlorida.match(oneOrMoreSsRegex);//['ss'];匹配连续出现零次或多次的字符使用星号。match(zeroOrMoreOsRegex);//["hi"]happyHi.match(zeroOrMoreOsRegex);//["hiiiiiii"]twoHis.match(zeroOrMoreOsRegex);//["hii","hii"]bye.match(zeroOrMoreOsRegex);//null惰性匹配字符串匹配给定要求的最小值正则表达式的一部分默认是贪心的(匹配字符串中满足给定要求的最长部分)使用?防止贪心模式(惰性匹配)catast"]testString.match(lazyRegex);//["cat"]匹配起始字符串模式要测试字符串开头的字符匹配,请使用插入符^,但扩大开头,不要放在字符集中constmmaAtFrontOfString="Emmalikescatsalot.";emma/;startingStringRegex.test(emmaAtFrontOfString);//truestartingStringRegex.test(emmaNotAtFrontOfString);//false匹配结束字符串方式使用$判断字符串是否以指定字符结尾conststartingStringRegex=/Emma$/;startingStringRegex.test(emmaAtBackOfString);//truestartingStringRegex.test(emmaNotAtBackOfString);//false匹配所有字母和数字使用\word简写constlongHand=/[A-Za-z0-9_]+/;constshortHand=/\w+/;constnumbers="42";constmyFavoriteColor="magenta";longHand.test(numbers);//trueshortHand.test(numbers);//truelongHand.test(myFavoriteColor);//trueshortHand.test(myFavoriteColor);//true除了字母和数字,其他必须匹配\W表示的\w的反义constnoAlphaNumericCharRegex=/\W/gi;constweirdCharacters="!_$!!";constalphaNumericCharacters="ab283AD";noAlphaNumericCharRegex.test(weirdCharacters);//truenoAlphaNumericCharRegex.test(alphaNumericCharacters);//false匹配所有数字你可以使用字符集[0-9],或者使用简写\dconstdigitsRegex=/\d/g;conststringWithDigits="Mycateats$20.00worthofffoodaweek.";stringWithDigits.match(digitsRegex);//["2","0","0","0"]匹配所有非数字with\D表示反义\dconstnonDigitsRegex=/\D/g;conststringWithLetters="101degrees";stringWithLetters.match(nonDigitsRegex);//["","d","e","g","r","e","e","s"]匹配空格使用\s匹配空格和回车constsentenceWithWhitespace="Ilikecats!"varspaceRegex=/\s/g;whiteSpace.match(sentenceWithWhitespace);//["",""]匹配非空格,用\S表示\s的反义词constsentenceWithWhitespace="Cat"constnonWhiteSpaceRegex=/\S/g;sentenceWithWhitespace.match(nonWhiteSpaceRegex);//["C","a","t"]匹配的字符数可以使用{lowerbound,upperbound}来指定一行中具体的字符数constregularHi="hi";constmediocreHi="hiii";constsuperExcitedHey="heeeeyyyyy!!!";constexcitedRegex=/hi{1,4}/;excitedRegex.test(regularHi);//trueexcitedRegex.test(mediocreHi);//trueexcitedRegex.test(superExcitedHey);//false匹配最大字符数使用{lowerbound,}来定义最小字符数要求,下例显示字母i必须至少出现2次falseexcitedRegex.test(mediocreHi);//trueexcitedRegex.test(superExcitedHey);//false匹配准确的字符数使用{requiredCount}指定准确需要的字符数constregularHi="hi";constbestHi="hii";constmediocreHi="hiii";constexcitedRegex=/hi{2}/;excitedRegex.test(regularHi);//falseexcitedRegex.test(bestHi);//trueexcitedRegex.test(mediocreHi);//false匹配0或1使用?匹配字符0或1constbritishSpelling="colour";constamericanSpelling="Color";constlanguageRegex=/colou?r/i;languageRegex.test(britishSpelling);//truelanguageRegex.test(americanSpelling);//真正的代码部署可能存在BUG我实时知道,后来为了解决这些BUG,我花了很多时间在日志调试上。顺便推荐一个好用的BUG监控工具Fundebug