i
Case-insensitive matching.
RegExp flags change how a pattern searches: case sensitivity, global matching, multiline anchors, Unicode behavior and more.
const pattern = /javascript/gi; const text = "JavaScript and javascript"; console.log(text.match(pattern));
Regular Expressions
Flags are placed after a regex literal or passed as the second argument to new RegExp. They change how matching behaves.
The most common flags are i for case-insensitive, g for global matching and m for multiline anchors. Unicode-heavy patterns often use u.
Some flags affect state. A global or sticky regex used with test repeatedly can remember lastIndex, which surprises many developers.
Case-insensitive matching.
Find all matches instead of only the first in many methods.
^ and $ match line boundaries.
Unicode-aware pattern behavior.
Examples
const wordPattern = /javascript/gi; const matches = text.match(wordPattern) ?? [];
const pattern = /ready/g;
pattern.test("ready");
pattern.test("ready");
// The second result can surprise you because lastIndex moved.
Code patterns
These examples are intentionally small. Regex becomes useful when each pattern has one clear job and a readable method around it.
Match regardless of letter casing.
const pattern = /javascript/i;
pattern.test("JavaScript");
Find every match with match or matchAll.
const text = "one two one"; console.log(text.match(/one/g));
Make ^ and $ work per line.
const text = "first\nsecond"; console.log(text.match(/^second/m));
Pass flags as the second argument.
const pattern = new RegExp("javascript", "i");
Rules that matter
Regular expressions are compact by design. Good JavaScript around the pattern is what makes them safe to maintain.
It avoids manual lowercasing for simple cases.
Without g many methods stop at the first match.
It changes ^ and $, not dot behavior.
It improves modern Unicode matching.
g and y can make regex objects stateful through lastIndex.
A pattern without its flags is only half the behavior.
Production thinking
Flags can change a regex from one match to many, or from case-sensitive to forgiving. They are not decoration.
Search features should behave predictably for different casing and multi-line text.
Production regex code should avoid reusing stateful global regexes in validation helpers unless lastIndex is controlled.
Case-insensitive slug or URL cleanup should be deliberate and tested.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Regex skill is not memorizing every token. It is knowing how to build small patterns and choose the right string method around them.