TExcelTAD

Flags

Flags (also called modifiers) change how the whole pattern behaves. In JavaScript they go after the closing slash — /abc/gi — or as the second argument of the constructor — new RegExp("abc", "gi"). Their order doesn't matter.

Flag Property Effect
g global find all matches, not just the first
i ignoreCase case-insensitive
m multiline ^ and $ match at every line
s dotAll . also matches line breaks
u unicode Unicode mode: code points, \p{...}, strict syntax
v unicodeSets upgraded u: set operations in classes, string properties
y sticky match only exactly at lastIndex
d hasIndices record start/end indices of each group

g — global

Without g, matching stops after the first hit. With it, you get every non-overlapping match. Both appear in the earlier lessons; in code the difference shows up clearly in match:

js · try it yourself
const s = "a1 b2 c3";
console.log(s.match(/\d/));
console.log(s.match(/\d/g));
[ '1', index: 1, input: 'a1 b2 c3', groups: undefined ]
[ '1', '2', '3' ]

i — ignore case

regex · try it yourself
/hello/gi
Hello HELLO hElLo help
3 matches
#1 "Hello" at index 0
#2 "HELLO" at index 6
#3 "hElLo" at index 12

m and s

These two are easy to confuse: m changes the anchors, s changes the dot.

regex · try it yourself
/^\w+:.*$/gm
name: Lan
note: multi
line text
age: 30
3 matches
#1 "name: Lan" at index 0
#2 "note: multi" at index 10
#3 "age: 30" at index 32
regex · try it yourself
/note:.*?(?=\n\w+:)/s
name: Lan
note: multi
line text
age: 30
1 match
#1 "note: multi\nline text" at index 10

u — Unicode mode

The u flag makes the engine work with code points instead of UTF-16 code units, enables \p{...} property escapes and \u{1F600} escapes, and makes the syntax stricter (unknown escapes become errors). Emoji outside the Basic Multilingual Plane are two code units long, so without u a dot matches only half of one:

js · try it yourself
const s = "🍜🍣";
console.log(s.length, s.match(/./g).length, s.match(/./gu).length);
console.log(/^.$/.test("🍜"), /^.$/u.test("🍜"));
4 4 2
false true

Tip: Turn on u (or v) for any pattern that handles user text. It costs nothing and avoids surrogate-pair surprises.

v — Unicode sets

v (ES2024) is a stricter superset of u. It adds set operations inside classes: && for intersection and -- for subtraction, plus nested classes.

regex · try it yourself
/[\p{L}--[a-zA-Z]]+/gv
naïve café Đà Nẵng
4 matches
#1 "ï" at index 2
#2 "é" at index 9
#3 "Đà" at index 11
#4 "ẵ" at index 15

That class reads "any letter, minus the ASCII letters" — so only the accented letters match. You can't combine u and v; pick one.

y — sticky

A sticky regex only tries to match at exactly lastIndex — it never scans forward. That makes it ideal for tokenizers, which consume input piece by piece:

js · try it yourself
const re = /\d+|[+*()-]|\s+/y;
const src = "12 + (3*4)";
const tokens = [];
while (re.lastIndex < src.length) {
  const m = re.exec(src);
  if (!m) { console.log("bad char at", re.lastIndex); break; }
  if (m[0].trim()) tokens.push(m[0]);
}
console.log(tokens.join(" "));
12 + ( 3 * 4 )

d — indices

With d, each match has an indices array holding [start, end] for the whole match and every group:

js · try it yourself
const m = /(?<key>\w+)=(?<val>\w+)/d.exec("set color=red");
console.log(m.indices[0], m.indices[1], m.indices[2]);
console.log(m.indices.groups.val);
[ 4, 13 ] [ 4, 9 ] [ 10, 13 ]
[ 10, 13 ]

Flags in other languages

JS Python Java Inline form (Python/Java/.NET/PCRE)
i re.I Pattern.CASE_INSENSITIVE (?i)
m re.M Pattern.MULTILINE (?m)
s re.S Pattern.DOTALL (?s)
— re.X Pattern.COMMENTS (?x)
g use findall/finditer/sub use a find() loop —

Test yourself

Which flag makes . match newline characters?

  1. m
  2. s
  3. g
  4. n

What does the y (sticky) flag do?

  1. Keeps matches after the first
  2. Only matches starting exactly at lastIndex
  3. Makes quantifiers lazy
  4. Ignores whitespace in the pattern

Why should you use u when matching emoji?

  1. Emoji are not allowed otherwise
  2. So . and classes work on whole code points instead of surrogate halves
  3. It makes matching case-insensitive
  4. It enables the g flag