FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Regular Expressions

Advanced

RegExp Flags

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 small letters with big behavior changes.

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.

i

Case-insensitive matching.

g

Find all matches instead of only the first in many methods.

m

^ and $ match line boundaries.

u

Unicode-aware pattern behavior.

Examples

Regex should be named, scoped and tested against edge cases.

Flags chosen for the exact search

const wordPattern = /javascript/gi;
const matches = text.match(wordPattern) ?? [];

Global test reused without understanding lastIndex

const pattern = /ready/g;

pattern.test("ready");
pattern.test("ready");

// The second result can surprise you because lastIndex moved.

Code patterns

Reusable examples for quick reference.

These examples are intentionally small. Regex becomes useful when each pattern has one clear job and a readable method around it.

Case-insensitive

Match regardless of letter casing.

const pattern = /javascript/i;
pattern.test("JavaScript");

Global matching

Find every match with match or matchAll.

const text = "one two one";
console.log(text.match(/one/g));

Multiline anchors

Make ^ and $ work per line.

const text = "first\nsecond";
console.log(text.match(/^second/m));

Constructor flags

Pass flags as the second argument.

const pattern = new RegExp("javascript", "i");

Rules that matter

Make the pattern purpose visible outside the pattern.

Regular expressions are compact by design. Good JavaScript around the pattern is what makes them safe to maintain.

Use i for casing

It avoids manual lowercasing for simple cases.

Use g when you need all matches

Without g many methods stop at the first match.

Use m only for multiline anchors

It changes ^ and $, not dot behavior.

Use u for Unicode-aware patterns

It improves modern Unicode matching.

Be careful with global test

g and y can make regex objects stateful through lastIndex.

Keep flags visible

A pattern without its flags is only half the behavior.

Production thinking

A regex without tests is a guess with punctuation.

Why does this matter?

Flags can change a regex from one match to many, or from case-sensitive to forgiving. They are not decoration.

Accessibility

Search features should behave predictably for different casing and multi-line text.

Production note

Production regex code should avoid reusing stateful global regexes in validation helpers unless lastIndex is controlled.

SEO note

Case-insensitive slug or URL cleanup should be deliberate and tested.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

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

Try this now.

  • Remove the i flag and compare results.
  • Remove the g flag and compare match counts.
  • Remove the m flag and test the CSS line.

Practice assignment

Do this before moving to the next topic.

  1. Use the i flag.
  2. Use the g flag.
  3. Use the m flag with a multi-line string.

Try it yourself

Compare regex flags

Live preview

Self-check

Before you continue, prove that you understand RegExp Flags.

Advanced

Regex skill is not memorizing every token. It is knowing how to build small patterns and choose the right string method around them.

  1. Can you explain i?
  2. Can you explain g?
  3. Can you explain m?
  4. Can you explain u?
  5. Can you explain why global regexes can be stateful?