Regex

Flags

  • g : Global. Searches the entire string. Without g, only the first match is returned.
  • i : Case Insensitive. Makes the entire regex case-insensitive.
  • m : Multiline. Causes ^ and $ to match the start and end of lines, not just the start and end of the string.
  • s : Single line (DotAll). Causes . to match newline characters as well.
  • u : Unicode. Treats the pattern as a sequence of Unicode code points.
  • y : Sticky. Matches at the exact position in the string specified by the lastIndex property (useful with loops).

Character Classes

  • . Matches any character (except newline) - abcABC123 .:!?
  • \d Matches any digit (0-9) - 1234567890
  • \D Matches any non-digit character - abcABC_~!@#$%^&*()-+=[]{}|;:',.<>/?
  • \w Matches any word character (letters, numbers, underscore) - abcABC_123
  • \W Matches any non-word character - ~!@_#$%^&*()-+=[]{}|;:',.<>
  • \s Matches any whitespace character (spaces, tabs, line breaks)
  • \S Matches any non-whitespace character (any visible character)
  • \b Matches a word boundary - \bcat\b matches cat
  • \B Matches a non-word boundary
  • \\ Escapes special characters

A word character is defined as any alphanumeric character (a-z, A-Z, 0-9) or an underscore (_).

Quantifiers (*, +, ?, {n}, {n,}, {n,m})

  • * Zero or more - \be*\ matches b be bee
  • + One or more - \be+\ matches be bee
  • ? Zero or one - \be*\ matches b be
  • {n} Exactly n times - \be{2}\ matches bee
  • {n,} n or more times - \be{2,}\ matches bee beee
  • {n,m} Between n and m times - \be{1,2}\ matches be bee

Assertions

Assertions are conditions that do not consume characters in the string, but they assert whether a match is possible or not. They are used to test if a string contains a certain pattern without including the pattern in the result.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Assertions

All examples below are referring to the string as abc123

  • (?=) Positive lookahead - x(?=y) matches x only when followed by y
  • (?!) Negative lookahead - x(?!y) matches x only when NOT followed by y
  • (?<=) Positive lookbehind - (?<=y)x matches x only when preceded by y
  • (?<!) Negative lookbehind - (?<!y)x matches x only when NOT preceded by y

Anchors

  • $ End of string or line - cat$ matches cat at the end of a string
  • \A Start of string - \Acat matches cat at the start of a string
  • \Z End of string - cat\Z matches cat at the end of a string
  • \b Matches a word boundary - In "cat dog", \bcat\b matches cat
  • \B Matches a non-word boundary. In "concatenate", \Bcat\B matches cat.
  • ^ Start of string or line

Character set or range [] and negated character set [^]

Use the square brackets [] to specify a character set or range. Alternatively, use square brackets with a caret [^] to define a negated character set. This set will match any character except those listed inside the brackets.


/b[aio]r/ - Finds all words that start with b, ends with r and has either a, i or o in between.
Output: bar ber bir bor bur

/b[^eu]r/ - Finds all words that start with b, ends with r and has any character except a, i, or o in the middle.
Negated Output: bar ber bir bor bur


/[a-f]/ - Finds all characters from a to f.
Output: abcdefghijklmnopqrstuvwxyz

/[^a-f]/ - Finds all characters that are not from a to f.
Negated Output: abcdefghijklmnopqrstuvwxyz


/[3-6]/ - Finds all numbers from 3 to 6.
Output: 0123456789

/[^3-6]/ - Finds all characters that are not numbers from 3 to 6.
Negated Output: 0123456789


[bdf]eer words that start with b, d, or f and end with eer
[^bdf]eer words that do not start with b, d, or f and end with eer

Examples

Search for words or character in a string

/e/g - The quick brown fox jumps over the lazy dog.
/the/g - The quick brown fox jumps over the lazy dog.
/the/gi - The quick brown fox jumps over the lazy dog.

Find words that start with ...

\b[bfla]\w* - words that start with b, f, l, or a
Mike likes to ride a bike like he is five again, not fifty.

\b[bfla]\w*e - words that start with b, f, l, or a and ends with e
Mike likes to ride a bike like he is five again, not fifty.

/\b[bdf]eer\b/g - beer deer feer
/\b[^bdf]eer\b/g - cheer jeer leer

Find all words except for. (Negated character set)

/\b[^t]he\b/g - The quick brown fox jumps over the lazy dog.
/\b[^t]he\b/gi - The quick brown fox jumps over the lazy dog.

Matching words that contain...

To find words with vowels in a text, you can use the regular expression \w*[aeiou]\w*.

\b\w*[aeiou]\w*\b when

\w*[aeiou]\w* matched Expression

matches any word that contains at least one vowel.

Here's what each part of the regex does:

  • \b.....\b matches a word boundary. This means it matches the position where a word character is not followed or preceded by another word character (like spaces, line breaks, punctuation, the start/end of a string, etc.).

\w* matches zero or more word characters (a word character is a letter, number, or underscore). [aeiou] matches any vowel. \w* matches zero or more word characters again. \b matches another word boundary. So, \b\w*[aeiou]\w*\b will match any word that contains at least one vowel, like "apple", "orange", "banana", etc.

JavaScript examples

Mozilla #using_regular_expressions_in_javascript

const text = "The quick brown fox jumps over the lazy dog.";
const regex = /e/g;

console.log(text.match(regex)); // ["e", "e", "e", "e", "e"]

Test for a valid Australian phone number

To pass the test, the phone number must start with 04 followed by 8 digits.

function isValidAustralianMobile(number) {
    const sanitizedNumber = number.replace(/\D/g, '');
    return /^04\d{8}$/.test(sanitizedNumber);
}

The sanitizedNumber variable strips non-digit characters from the input, allowing the function to accept phone numbers in various formats, provided they start with 04 and are followed by 8 digits.

  • ^04 asserts the start of a line begins with exactly 04.
  • \d{8} matches exactly eight digits.
  • $ asserts the end of a line.

Additional Resources