Regular Expressions
100 flashcards by MentisLibrary
Tokens, quantifiers, groups, and lookarounds, plus the common patterns you will reach for again and again.
- What does the . (dot) metacharacter match?
- Any single character except a newline
- What does the * quantifier mean?
- Zero or more of the preceding element
- What does the + quantifier mean?
- One or more of the preceding element
- What does the ? quantifier mean?
- Zero or one of the preceding element (optional)
- What does the quantifier {3} mean?
- Exactly 3 repetitions of the preceding element
- What does the quantifier {2,4} mean?
- Between 2 and 4 repetitions (inclusive)
- What is the difference between greedy and lazy quantifiers?
- Greedy takes as much as possible; lazy (with a trailing ?) takes as little as possible
- What does \d match?
- Any digit 0-9
- What does \w match?
- A word character: letter, digit, or underscore
- What does \s match?
- Any whitespace character: space, tab, newline, and friends
- What does the ^ anchor mean?
- The start of the string (or of a line with the m flag)
- What does the $ anchor mean?
- The end of the string (or of a line with the m flag)
- What does \b match?
- A word boundary: the position between a word character and a non-word character
- What does a character class like [abc] match?
- Exactly one character: a, b, or c
- What does the negated class [^0-9] match?
- Any single character that is not a digit
- What does the range class [a-fA-F0-9] match?
- One hexadecimal digit, either case
- What does the | operator do?
- Alternation: matches either the expression on its left or on its right
- What do parentheses ( ) do in a regex?
- Group elements together and capture the matched text
- What is a non-capturing group?
- (?:...) groups without storing a capture
- What is a named capture group?
- (?<name>...) captures under a readable name instead of a number
- What is a backreference?
- A reference to an earlier capture inside the same pattern, like \1
- What is a positive lookahead?
- (?=...) asserts that something follows, without consuming it
- What is a negative lookahead?
- (?!...) asserts that something does NOT follow
- What is a positive lookbehind?
- (?<=...) asserts that something precedes, without consuming it
- What is a negative lookbehind?
- (?<!...) asserts that something does NOT precede
- What does the i flag do?
- Makes matching case-insensitive
- What does the g flag do in JavaScript?
- Global: finds all matches instead of stopping at the first
- What does the m (multiline) flag change?
- ^ and $ match at the start and end of every line, not just the whole string
- What does the s (dotall) flag change?
- Makes . match newline characters too
- How do you match a literal special character like . or + or (?
- Escape it with a backslash