Concept
Regex Flags in JavaScript: g, i, m, s, u, y
Flags modify regex behavior globally. g enables global matching, i makes it case-insensitive, m changes anchor behavior, s dots match newlines, u enables Unicode, y is sticky.
g — global
Without g, methods like String.match return only the first match. With g, they return all matches. String.replace and String.replaceAll use g to process every occurrence. Critical for extraction use cases.
i — case-insensitive
/hello/i matches 'Hello,' 'HELLO,' and 'hELLO.' The i flag applies to the whole expression — you can't flip it on for part of a regex without modern inline modifiers.
m — multiline
Changes ^ and $ to match at the start and end of each LINE in addition to the start/end of the string. Essential for log parsing and any line-by-line processing in a single input buffer.
s — dotall (single-line)
Without s, the dot . matches any character EXCEPT a newline. With s, it matches newlines too. The name is confusing — 'single-line' mode means the engine treats the input as one big line.
u and y
u enables full Unicode matching — \p{...} properties, surrogate pair handling, and stricter escape rules. Essential for any non-ASCII text. y (sticky) forces the match to start exactly at the lastIndex position of the regex object — useful for tokenizers that advance through input sequentially.
Related patterns
URL Validation
Match http and https URLs with optional www prefix, paths, query strings, and fragments.
/https?:\/\/(?:www\.)?[\w\-]+(?:\.[…/giUUID (v1–v5)
Match RFC 4122 UUIDs (versions 1–5) in the standard 8-4-4-4-12 hex format.
/[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a…/giHTML Tag Matcher
Match paired HTML tags and capture the tag name and inner content using a back-reference.
/<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>([\…/gHTML Comment
Match HTML comments, including multi-line comments and empty ones.
/<!--[\s\S]*?-->/g