Regular expression cheat sheet

    • Testing regular expressions
    • Testing multiple modes
    • Ignore case
    • Extract the first match to the variable
    • Extracts all matches in the array
    • Match any character
    • There are many possibilities for matching a single role
    • Match the letter
    • Matches specific numbers and letters
    • Matches an unknown character
    • Matches characters that appear one or more times in a row
    • Matches characters that occur zero or more times in a row
    • Delayed match
    • Matches the start string pattern
    • Matches the ending string pattern
    • Matches all letters and numbers
    • Matches everything except letters and numbers
    • Match all numbers
    • Matches all non-numbers
    • Match the blank space
    • Match non-space
    • Number of matching characters
    • Minimum number of matched characters
    • The number of characters that match the exact number
    • Matches all or none of the characters

Regular expressions or “regex” are used to match parts of a string. Here’s the cheat sheet I used to create regular expressions.


Testing regular expressions

  • use.test()methods
let testString = "My test string";
let testRegex = /string/;
testRegex.test(testString);
Copy the code

Testing multiple modes

  • Using the OR operator (|)
const regex = /yes|no|maybe/;
Copy the code

Ignore case

  • The use of theiFlag to be case sensitive
const caseInsensitiveRegex = /ignore case/i;
const testString = 'We use the i flag to iGnOrE CasE';
caseInsensitiveRegex.test(testString); // true
Copy the code

Extract the first match to the variable

  • use.match()function
const match = "Hello World!".match(/hello/i); // "Hello"
Copy the code

Extracts all matches in the array

  • usegmark
const testString = "Repeat repeat rePeAT";
const regexWithAllMatches = /Repeat/gi;
testString.match(regexWithAllMatches); // ["Repeat", "repeat", "rePeAT"]
Copy the code

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"]
Copy the code

There are many possibilities for matching a single role

  • Use the character class, which allows you to define a set of characters that you want to match
  • You 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"]
Copy the code

Match the letter

  • Use a range within the character set[a-z]
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
Copy the code

Matches specific numbers and letters

  • You can also use a hyphen to match numbers
const regexWithLetterAndNumberRange = /[a-z0-9]/ig;
const testString = "Emma19382";
testString.match(regexWithLetterAndNumberRange) // true
Copy the code

Matches an unknown character

  • To match an unwanted character set, use the reverse character set
  • To negate character sets, use caret^
const allCharsNotVowels = /[^aeiou]/gi;
const allCharsNotVowelsOrNumbers = /[^aeiou0-9]/gi;
Copy the code

Matches characters that appear one or more times in a row

  • use+symbol
const oneOrMoreAsRegex = /a+/gi;
const oneOrMoreSsRegex = /s+/gi;
const cityInFlorida = "Tallahassee";

cityInFlorida.match(oneOrMoreAsRegex); // ['a', 'a', 'a'];
cityInFlorida.match(oneOrMoreSsRegex); // ['ss'];
Copy the code

Matches characters that occur zero or more times in a row

  • Use the 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
Copy the code

Delayed match

  • The smallest part of a string that meets a given requirement
  • By default, regular expressions are greedy (matching the longest part of a string that satisfies a given requirement)
  • use?Character for lazy matching
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"]
Copy the code

Matches the start string pattern

  • To test for a match at the beginning of the string, use the caret^, but outside the character set
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
Copy the code

Matches the ending string pattern

  • $Use the dollar sign at the end of the regular expression to check for patterns at the end of the string
const emmaAtBackOfString = "The cats do not like Emma";
const emmaNotAtBackOfString = "Emma loves the cats";
const startingStringRegex = /Emma$/;

startingStringRegex.test(emmaAtBackOfString); // true
startingStringRegex.test(emmaNotAtBackOfString); // false
Copy the code

Matches all letters and numbers

  • use\wordshorthand
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
Copy the code

Matches everything except letters and numbers

  • You can use\wThe opposite of the\WUsed together
const noAlphaNumericCharRegex = /\W/gi;
const weirdCharacters = ! "" _ $!!!!!";
const alphaNumericCharacters = "ab283AD";

noAlphaNumericCharRegex.test(weirdCharacters); // true
noAlphaNumericCharRegex.test(alphaNumericCharacters); // false
Copy the code

Match all numbers

  • You can use character sets[0-9]You can also use shorthand\d
const digitsRegex = /\d/g;
const stringWithDigits = "My cat eats $20.00 worth of food a week.";

stringWithDigits.match(digitsRegex); // ["2", "0", "0", "0"]
Copy the code

Matches all non-numbers

  • You can use\dThe opposite of the\DUsed together
const nonDigitsRegex = /\D/g;
const stringWithLetters = "101 degrees";

stringWithLetters.match(nonDigitsRegex); // [" ", "d", "e", "g", "r", "e", "e", "s"]
Copy the code

Match the blank space

  • use\sMatches Spaces and carriage returns
const sentenceWithWhitespace = "I like cats!"
var spaceRegex = /\s/g;
whiteSpace.match(sentenceWithWhitespace); // [" ", ""]
Copy the code

Match non-space

  • You can use\sThe opposite of the\SUsed together
const sentenceWithWhitespace = "C a t"
const nonWhiteSpaceRegex = /\S/g;
sentenceWithWhitespace.match(nonWhiteSpaceRegex); // ["C", "a", "t"]
Copy the code

Number of matching characters

  • You can use it to specify a specific number of characters in a line{lowerBound, upperBound}
const regularHi = "hi";
const mediocreHi = "hiii";
const superExcitedHey = "heeeeyyyyy!!!";
const excitedRegex = {1, 4} / / hi;

excitedRegex.test(regularHi); // true
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false
Copy the code

Minimum number of matched characters

  • You can only define the minimum number of character requirements using the following command{lowerBound,}
  • This is called a quantity specifier
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
Copy the code

The number of characters that match the exact number

  • You can specify the exact number of characters required using the following command{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
Copy the code

Matches all or none of the characters

  • To check if a character exists, use the?
const britishSpelling = "colour";
const americanSpelling = "Color";
const languageRegex = /colou? r/i;

languageRegex.test(britishSpelling); // true
languageRegex.test(americanSpelling); // true
Copy the code

Author’s Official Account:What do you want biu to order

Pay attention to support it, I will continue to update free fun H5 small game code, Java small game code, fun, practical projects and software, learning resources and so on.

Thank you for reading to the end ❤️ here is your medal



Don’t forget to support ❤️ or 📑