Combining Multiple Flags
You'll learn: How to use multiple regular expression flags together and understand that their order doesn't matter.
The Core Idea
Regular expression flags work independently of each other and can be combined freely. When you need multiple behaviors—like case-insensitive global matching, or multiline unicode-aware searching—you simply include all the flags you need. The order you write them in makes no difference; /pattern/gim works identically to /pattern/mig.
How It Works
In literal notation, just concatenate the flags:
const pattern = /hello/gi; // global + case-insensitive
With the RegExp constructor, pass them as a single string:
const pattern = new RegExp('hello', 'gi');
Common Combinations
Global + Case-Insensitive (gi): Find all matches regardless of case
const text = "Hello world, hello everyone";
const matches = text.match(/hello/gi);
console.log(matches); // ['Hello', 'hello']
Multiline + Global (gm): Find patterns across multiple lines
const lines = "start\nmiddle\nend";
const matches = lines.match(/^[a-z]+$/gm);
console.log(matches); // ['start', 'middle', 'end']
Unicode + Case-Insensitive (ui): Handle Unicode characters properly while ignoring case
const text = "Café café CAFÉ";
const matches = text.match(/café/gui);
console.log(matches); // All three variations
Order Independence
These are all equivalent:
/pattern/gim/pattern/mig/pattern/img
The regex engine applies all specified flags regardless of their order.
Key Takeaway: Combine flags freely by listing them together—their order doesn't matter, and each flag's behavior works independently alongside the others.