/
📝

Regular Expression-A pattern describing a certain amount of text.

https://www.regular-expressions.info/quickstart.html?wlr=1
regex
On this page

Playground

A regular expression, or regex for short, is a pattern describing a certain amount of text.

A valid regex consists of alphanumeric characters representing the set of input symbols (e.g. a, B, 9), the $ character representing the empty string, the choice operator +, the Kleene operator s, and parentheses ( and ). An example of a valid regex is: (a+B)_(c9+$)+$.

Cheat Sheet

  • * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

  • .* matches any character (except for line terminators)

  • \d+ means one or more numbers

  • (\d{3})(\d{4})(\d{4})

    • 1st Capturing Group (\d{3})
      • \d{3} matches a digit (equal to [0-9])
      • {3} Quantifier — Matches exactly 3 times
  • Dates: (\d{2})-(\d{2})-(\d{4})

    • Change order: $3-$2-$1
    • named capture groups
      • /(?<month>\d{2})-(?<day>\d{2})-(?<year>\d{4})/
      • $<day>-$<month>-$<year>
  • replace different strings with the same string /color: (rgb\(255, 255, 255\)|#f{6}}|#fff|white)/g, 'color: #000')

  • Modifier

    • gi: means global, case-insensitive,
  • Multipliers

    • * - item occurs zero or more times(greedy, as many times as possible)..
    • + - item occurs one or more times(greedy).
    • ? - item occurs zero or one times(lazy, as few times as possible).
    • {5} - item occurs five times.
    • {3,7} - item occurs between 3 and 7 times.
  • l.*k: Are you looking at the lock or the silk? (greedy)

  • l.*?k: Are you looking at the lock or the silk? (lazy)

  • Add '' to all links, replace link: (.*) with link: $1

Check for prime numbers

/^1?$|^(11+?)\1+$/

Basic

Literal Characters

Twelve characters have special meanings in regular expressions: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {. These special characters are often called “metacharacters”. Most of them are errors when used alone.

Character Classes or Character Sets

To match an a or an e, use [ae]. You could use this in gr[ae]y to match either gray or grey.

A character class matches only a single character. gr[ae]y does not match graay, graey or any such thing.

Tools

Regular Expressions Gym

Simplify a regular expression.

Resources

Awesome Regex

Useful Regular Expressions

Edit this page
logo
Code-related notes and snippets