In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use regular expressions to implement a cheat sheet in JavaScript? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Matching regularity
Use the .test () method
Let testString = "My test string"; let testRegex = / string/;testRegex.test (testString)
Match multiple patterns
Use action symbols |
Const regex = / yes | no | maybe/
Ignore case
Use the I flag to ignore case
Const caseInsensitiveRegex = / ignore case/i;const testString ='We use the i flag to iGnOrE CasE';caseInsensitiveRegex.test (testString); / / true
Extract the first match of a variable
Use the .match () method
Const match = "Hello World!" .match (/ hello/i); / / "Hello"
Extract all matches in the array
Use the g flag
Const testString = "Repeat repeat rePeAT"; const regexWithAllMatches = / Repeat/gi;testString.match (regexWithAllMatches); / / ["Repeat", "repeat", "rePeAT"]
Match any character
Use wildcards. As a placeholder for any character
/ / 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"]
Match a single character with multiple possibilities
Using a character class, you can use it to define a set of characters to match
Put them in square brackets []
/ match "cat"fat" and "mat" but not "bat" const regexWithCharClass = / [cfm] at/g;const testString = "cat fat bat mat"; const allMatchingWords = testString.match (regexWithCharClass); / / ["cat", "fat", "mat"]
Match the letters in the alphabet
Use the range within the character set [amurz]
Const regexWidthCharRange = / [a mure] at/;const regexWithCharRange = / [a mure] at/;const catString = "cat"; const batString = "bat"; const fatString = "fat"; regexWithCharRange.test (catString); / / trueregexWithCharRange.test (batString); / / trueregexWithCharRange.test (fatString); / / false
Match specific numbers and letters
You can also use hyphens to match numbers
Const regexWithLetterAndNumberRange = / [a-z0-9] / ig;const testString = "Emma19382"; testString.match (regexWithLetterAndNumberRange) / / true
Match a single unknown character
To match a set of characters you don't want to have, use the negative character set ^
Const allCharsNotVowels = / [^ aeiou] / gi;const allCharsNotVowelsOrNumbers = / [^ aeiou0-9] / gi
Matches characters that appear one or more times on a line
Use the + flag
Const oneOrMoreAsRegex = / cityInFlorida.match ss' oneOrMoreSsRegex = / cityInFlorida.match (oneOrMoreSsRegex); / / [ss']
Matches characters that appear zero or more times in a row
Use an asterisk *
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
Lazy matching
The smallest part of a string that matches a given requirement
By default, regular expressions are greedy (matching the longest part of a string that meets a given requirement)
Use? Block greedy mode (lazy matching)
Const testString = "catastrophe"; const greedyRexex = / c [a Murz] * t Unigo; const lazyRegex = / c [a Muyz] *? t Maple g; testString.match (greedyRexex); / / ["catast"] testString.match (lazyRegex); / / ["cat"]
Match starting string pattern
To test for character matching at the beginning of a string, use the caret ^, but to enlarge the beginning, do not put it in the character set
Const emmaAtFrontOfString = "Emma likes cats a lot."; const emmaNotAtFrontOfString = "The cats Emma likes are fluffy."; const startingStringRegex = / ^ Emma/;startingStringRegex.test (emmaAtFrontOfString); / / truestartingStringRegex.test (emmaNotAtFrontOfString); / / false
Match end string pattern
Use $to determine whether a string ends with a specified character
Const emmaAtBackOfString = "The cats do not like Emma"; const emmaNotAtBackOfString = "Emma loves the cats"; const startingStringRegex = / Emma$/;startingStringRegex.test (emmaAtBackOfString); / / truestartingStringRegex.test (emmaNotAtBackOfString); / / false
Match all letters and numbers
Use the abbreviation\ word
Const longHand = / [A-Za-z0-9 _] + /; const shortHand = /\ wishbank Const numbers = "42"; const myFavoriteColor = "magenta"; longHand.test (numbers); / / trueshortHand.test (numbers); / / truelongHand.test (myFavoriteColor); / / trueshortHand.test (myFavoriteColor); / / true
Everything has to match except letters and numbers
Use\ W to represent the antonym of\ w
Const noAlphaNumericCharRegex = /\ WGI _ Const weirdCharacters = "! _ $!"; const alphaNumericCharacters = "ab283AD"; noAlphaNumericCharRegex.test (weirdCharacters); / / truenoAlphaNumericCharRegex.test (alphaNumericCharacters); / / false
Match all numbers
You can use the character set [0-9], or use the abbreviation\ d
Const digitsRegex = /\ d Compact Const stringWithDigits = "My cat eats $20.00 worth of food a week."; stringWithDigits.match (digitsRegex); / / ["2", "0", "0", "0"]
Match all non-digits
Use\ D to represent the antonym of\ d
Const nonDigitsRegex = /\ DUnix Const stringWithLetters = "101 degrees"; stringWithLetters.match (nonDigitsRegex); / / ["", "d", "e", "g", "r", "e", "e", "s"]
Match Spac
Use\ s to match spaces and carriage returns
Const sentenceWithWhitespace = "I like cats!" var spaceRegex = /\ Gram GTX whiteSpace.match (sentenceWithWhitespace); / / [","]
Match non-space
Use\ S to represent the antonym of\ s
Const sentenceWithWhitespace = "C at" const nonWhiteSpaceRegex = /\ S hand box sentenceWithWhitespace.match (nonWhiteSpaceRegex); / / ["C", "a", "t"]
Number of characters matched
You can use {lower bound, upper bound} to specify a specific number of characters in a line
Const regularHi = "hi"; const mediocreHi = "hiii"; const superExcitedHey = "heeeeyyyyybeans!"; const excitedRegex = / hi {1je 4} /; excitedRegex.test (regularHi); / / trueexcitedRegex.test (mediocreHi); / / trueexcitedRegex.test (superExcitedHey); / / false
Number of characters that match the lowest number of characters
Use {lower bound,} to define a minimum number of character requirements. The following example indicates that the letter I must appear at least twice.
Const regularHi = "hi"; const mediocreHi = "hiii"; const superExcitedHey = "heeeeyyyyybeans!"; const excitedRegex = / hi {2,} /; excitedRegex.test (regularHi); / / falseexcitedRegex.test (mediocreHi); / / trueexcitedRegex.test (superExcitedHey); / / false
Match the exact number of characters
Use {requiredCount} to specify the exact number of characters required
Const regularHi = "hi"; const bestHi = "hii"; const mediocreHi = "hiii"; const excitedRegex = / hi {2} /; excitedRegex.test (regularHi); / / falseexcitedRegex.test (bestHi); / / trueexcitedRegex.test (mediocreHi); / / false
Match 0 times or 1 time
Use? Match characters 0 or 1 times
Const britishSpelling = "colour"; const americanSpelling = "Color"; const languageRegex = / colou?r/i;languageRegex.test (britishSpelling); / / truelanguageRegex.test (americanSpelling); / / true after reading the above, do you know how to use regular expressions to implement a cheat sheet in JavaScript? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.