¡¡¡¡Æ¥ÅäÕýÔò
¡¡¡¡Ê¹Óà .test() ·½·¨
- let testString = "My test string";
- let testRegex = /string/;
- testRegex.test(testString);
¡¡¡¡Æ¥Åä¶à¸öģʽ
¡¡¡¡Ê¹ÓòÙ×÷·ûºÅ |
- const regex = /yes|no|maybe/;
¡¡¡¡ºöÂÔ´óСд
¡¡¡¡Ê¹ÓÃi±êÖ¾±íʾºöÂÔ´óСд
- const caseInsensitiveRegex = /ignore case/i;
- const testString = 'We use the i flag to iGnOrE CasE';
- caseInsensitiveRegex.test(testString); // true
¡¡¡¡ÌáÈ¡±äÁ¿µÄµÚÒ»¸öÆ¥ÅäÏî
¡¡¡¡Ê¹Óà .match() ·½·¨
- const match = "Hello World!".match(/hello/i); // "Hello"
¡¡¡¡ÌáÈ¡Êý×éÖеÄËùÓÐÆ¥ÅäÏî
¡¡¡¡Ê¹Óà g ±êÖ¾
- const testString = "Repeat repeat rePeAT";
- const regexWithAllMatches = /Repeat/gi;
- testString.match(regexWithAllMatches); // ["Repeat", "repeat", "rePeAT"]
¡¡¡¡Æ¥ÅäÈÎÒâ×Ö·û
¡¡¡¡Ê¹ÓÃͨÅä·û. ×÷ΪÈκÎ×Ö·ûµÄռλ·û
- // To match "cat", "BAT", "fAT", "mat"
- const regexWithWildcard = /.at/gi;
- const testString = "cat BAT cupcake fAT mat dog";
- const allMatchingWords = testString.match(regexWithWildcard); // ["cat", "BAT", "fAT", "mat"]
¡¡¡¡ÓöàÖÖ¿ÉÄÜÐÔÆ¥Åäµ¥¸ö×Ö·û
¡¡¡¡Ê¹ÓÃ×Ö·ûÀ࣬Äã¿ÉÒÔʹÓÃËüÀ´¶¨ÒåҪƥÅäµÄÒ»×é×Ö·û
¡¡¡¡°ÑËüÃÇ·ÅÔÚ·½À¨ºÅÀï []
- //Æ¥Åä "cat" "fat" and "mat" µ«²»Æ¥Åä "bat"
- const regexWithCharClass = /[cfm]at/g;
- const testString = "cat fat bat mat";
- const allMatchingWords = testString.match(regexWithCharClass); // ["cat", "fat", "mat"]
¡¡¡¡Æ¥Åä×Öĸ±íÖеÄ×Öĸ
¡¡¡¡Ê¹ÓÃ×Ö·û¼¯Äڵķ¶Î§ [a-z]
- const regexWidthCharRange = /[a-e]at/;
- const regexWithCharRange = /[a-e]at/;
- const catString = "cat";
- const batString = "bat";
- const fatString = "fat";
- regexWithCharRange.test(catString); // true
- regexWithCharRange.test(batString); // true
- regexWithCharRange.test(fatString); // false
¡¡¡¡Æ¥ÅäÌض¨µÄÊý×ÖºÍ×Öĸ
¡¡¡¡Ä㻹¿ÉÒÔʹÓÃÁ¬×Ö·ûÀ´Æ¥ÅäÊý×Ö
- const regexWithLetterAndNumberRange = /[a-z0-9]/ig;
- const testString = "Emma19382";
- testString.match(regexWithLetterAndNumberRange) // true
¡¡¡¡Æ¥Åäµ¥¸öδ֪×Ö·û
¡¡¡¡ÒªÆ¥ÅäÄú²»ÏëÓµÓеÄÒ»×é×Ö·û£¬Ê¹Ó÷ñ¶¨×Ö·û¼¯ ^
- const allCharsNotVowels = /[^aeiou]/gi;
- const allCharsNotVowelsOrNumbers = /[^aeiou0-9]/gi;
¡¡¡¡Æ¥ÅäÒ»ÐÐÖгöÏÖÒ»´Î»ò¶à´ÎµÄ×Ö·û
¡¡¡¡Ê¹Óà + ±êÖ¾
- const oneOrMoreAsRegex = /a+/gi;
- const oneOrMoreSsRegex = /s+/gi;
- const cityInFlorida = "Tallahassee";
- cityInFlorida.match(oneOrMoreAsRegex); // ['a', 'a', 'a'];
- cityInFlorida.match(oneOrMoreSsRegex); // ['ss'];
¡¡¡¡Æ¥ÅäÁ¬Ðø³öÏÖÁã´Î»ò¶à´ÎµÄ×Ö·û
¡¡¡¡Ê¹ÓÃÐǺŠ*
- const zeroOrMoreOsRegex = /hi*/gi;
- const normalHi = "hi";
- const happyHi = "hiiiiii";
- const twoHis = "hiihii";
- const bye = "bye";
- normalHi.match(zeroOrMoreOsRegex); // ["hi"]
- happyHi.match(zeroOrMoreOsRegex); // ["hiiiiii"]
- twoHis.match(zeroOrMoreOsRegex); // ["hii", "hii"]
- bye.match(zeroOrMoreOsRegex); // null
¡¡¡¡¶èÐÔÆ¥Åä
¡¡¡¡×Ö·û´®ÖÐÓë¸ø¶¨ÒªÇóÆ¥ÅäµÄ×îС²¿·Ö
¡¡¡¡Ä¬ÈÏÇé¿öÏ£¬ÕýÔò±í´ïʽÊÇÌ°À·µÄ£¨Æ¥ÅäÂú×ã¸ø¶¨ÒªÇóµÄ×Ö·û´®µÄ×²¿·Ö£©
¡¡¡¡Ê¹Óà ? ×ḛֹ̀À·Ä£Ê½(¶èÐÔÆ¥Åä )
- const testString = "catastrophe";
- const greedyRexex = /c[a-z]*t/gi;
- const lazyRegex = /c[a-z]*?t/gi;
- testString.match(greedyRexex); // ["catast"]
- testString.match(lazyRegex); // ["cat"]
¡¡¡¡Æ¥ÅäÆðʼ×Ö·û´®Ä£Ê½
¡¡¡¡Òª²âÊÔ×Ö·û´®¿ªÍ·µÄ×Ö·ûÆ¥Å䣬ÇëʹÓòåÈë·ûºÅ^£¬µ«Òª·Å´ó¿ªÍ·£¬²»Òª·Åµ½×Ö·û¼¯ÖÐ
- const emmaAtFrontOfString = "Emma likes cats a lot.";
- const emmaNotAtFrontOfString = "The cats Emma likes are fluffy.";
- const startingStringRegex = /^Emma/;
- startingStringRegex.test(emmaAtFrontOfString); // true
- startingStringRegex.test(emmaNotAtFrontOfString); // false
¡¡¡¡Æ¥Åä½áÊø×Ö·û´®Ä£Ê½
¡¡¡¡Ê¹Óà $ À´ÅжÏ×Ö·û´®ÊÇ·ñÊÇÒԹ涨µÄ×Ö·û½áβ
- onst emmaAtBackOfString = "The cats do not like Emma";
- const emmaNotAtBackOfString = "Emma loves the cats";
- const startingStringRegex = /Emma$/;
- startingStringRegex.test(emmaAtBackOfString); // true
- startingStringRegex.test(emmaNotAtBackOfString); // false
¡¡¡¡Æ¥ÅäËùÓÐ×ÖĸºÍÊý×Ö
¡¡¡¡Ê¹ÓÃ\word ¼òд
- const longHand = /[A-Za-z0-9_]+/;
- const shortHand = /\w+/;
- const numbers = "42";
- const myFavoriteColor = "magenta";
- longHand.test(numbers); // true
- shortHand.test(numbers); // true
- longHand.test(myFavoriteColor); // true
- shortHand.test(myFavoriteColor); // true
¡¡¡¡³ýÁË×ÖĸºÍÊý×Ö£¬ÆäËûµÄ¶¼ÒªÆ¥Åä
¡¡¡¡ÓÃ\W ±íʾ \w µÄ·´Òå
- const noAlphaNumericCharRegex = /\W/gi;
- const weirdCharacters = "!_$!!";
- const alphaNumericCharacters = "ab283AD";
- noAlphaNumericCharRegex.test(weirdCharacters); // true
- noAlphaNumericCharRegex.test(alphaNumericCharacters); // false
¡¡¡¡Æ¥ÅäËùÓÐÊý×Ö
¡¡¡¡Äã¿ÉÒÔʹÓÃ×Ö·û¼¯[0-9]£¬»òÕßʹÓüòд \d
- const digitsRegex = /\d/g;
- const stringWithDigits = "My cat eats $20.00 worth of food a week.";
- stringWithDigits.match(digitsRegex); // ["2", "0", "0", "0"]
¡¡¡¡Æ¥ÅäËùÓзÇÊý×Ö
¡¡¡¡ÓÃ\D ±íʾ \d µÄ·´Òå
- const nonDigitsRegex = /\D/g;
- const stringWithLetters = "101 degrees";
- stringWithLetters.match(nonDigitsRegex); // [" ", "d", "e", "g", "r", "e", "e", "s"]
¡¡¡¡Æ¥Åä¿Õ¸ñ
¡¡¡¡Ê¹Óà \s À´Æ¥Åä¿Õ¸ñºÍ»Ø³µ·û
- const sentenceWithWhitespace = "I like cats!"
- var spaceRegex = /\s/g;
- whiteSpace.match(sentenceWithWhitespace); // [" ", " "]
¡¡¡¡Æ¥Åä·Ç¿Õ¸ñ
¡¡¡¡ÓÃ\S ±íʾ \s µÄ·´Òå
- const sentenceWithWhitespace = "C a t"
- const nonWhiteSpaceRegex = /\S/g;
- sentenceWithWhitespace.match(nonWhiteSpaceRegex); // ["C", "a", "t"]
¡¡¡¡Æ¥ÅäµÄ×Ö·ûÊý
¡¡¡¡Äã¿ÉÒÔʹÓà {Ͻ磬ÉϽç} Ö¸¶¨Ò»ÐÐÖеÄÌض¨×Ö·ûÊý
- const regularHi = "hi";
- const mediocreHi = "hiii";
- const superExcitedHey = "heeeeyyyyy!!!";
- const excitedRegex = /hi{1,4}/;
- excitedRegex.test(regularHi); // true
- excitedRegex.test(mediocreHi); // true
- excitedRegex.test(superExcitedHey); //false
¡¡¡¡Æ¥Åä×îµÍ¸öÊýµÄ×Ö·ûÊý
¡¡¡¡Ê¹ÓÃ{Ͻç, }¶¨Òå×îÉÙÊýÁ¿µÄ×Ö·ûÒªÇó,ÏÂÃæʾÀý±íʾ×Öĸ i ÖÁÉÙÒª³öÏÖ2´Î
- const regularHi = "hi";
- const mediocreHi = "hiii";
- const superExcitedHey = "heeeeyyyyy!!!";
- const excitedRegex = /hi{2,}/;
- excitedRegex.test(regularHi); // false
- excitedRegex.test(mediocreHi); // true
- excitedRegex.test(superExcitedHey); //false
¡¡¡¡Æ¥Å侫ȷµÄ×Ö·ûÊý
¡¡¡¡Ê¹ÓÃ{requiredCount}Ö¸¶¨×Ö·ûÒªÇóµÄÈ·ÇÐÊýÁ¿
- const regularHi = "hi";
- const bestHi = "hii";
- const mediocreHi = "hiii";
- const excitedRegex = /hi{2}/;
- excitedRegex.test(regularHi); // false
- excitedRegex.test(bestHi); // true
- excitedRegex.test(mediocreHi); //false
¡¡¡¡Æ¥Åä0´Î»ò1´Î
¡¡¡¡Ê¹Óà ? Æ¥Åä×Ö·û 0 ´Î»ò1´Î
- const britishSpelling = "colour";
- const americanSpelling = "Color";
- const languageRegex = /colou?r/i;
- languageRegex.test(britishSpelling); // true
- languageRegex.test(americanSpelling); // true
¡¡¡¡´úÂ벿Êðºó¿ÉÄÜ´æÔÚµÄBUGû·¨ÊµÊ±ÖªµÀ£¬ÊºóΪÁ˽â¾öÕâЩBUG£¬»¨ÁË´óÁ¿µÄʱ¼ä½øÐÐlog µ÷ÊÔ£¬Õâ±ß˳±ã¸ø´ó¼ÒÍƼöÒ»¸öºÃÓõÄBUG¼à¿Ø¹¤¾ß Fundebug¡£
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-1128-1.html