In JavaScript, regex.sticky is a read-only boolean. It is true when the RegExp was created with the y flag.
A sticky regex must match exactly atlastIndex. It does not search ahead in the string. That makes it useful for parsers that walk text position by position.
Syntax
Create the regex with y, set lastIndex, then match.
JavaScript
const re = /cat/y;
console.log(re.sticky); // true
re.lastIndex = 4;
console.log(re.exec("the cat")[0]); // "cat"
re.lastIndex = 0;
console.log(re.exec("the cat")); // null — "cat" is not at index 0
Call
What it does
/cat/y
Sticky matching. sticky is true.
re.lastIndex = n
Choose the exact index where the next match must start.
re.exec(str)
Succeeds only if the match begins at lastIndex.
Remember this at a glance:
Remember
/cat/.sticky → false
/cat/y.sticky → true
sticky: match only at lastIndex (no search ahead)
global: find next match anywhere after lastIndex
set lastIndex before you call exec() or test()
What You Get
Name
Description
regex.sticky
Read-only. true if the y flag was set when the RegExp was created.
y flag
Set at creation with /…/y or new RegExp(pattern, "y").
lastIndex
The exact index where the sticky match must begin.
Return Value
Value
Description
true
The RegExp has the y (sticky) flag.
false
The RegExp does not have the y flag.
JavaScript Examples
Example 1 — Read sticky
See how the y flag sets the property.
JavaScript
const a = /cat/;
const b = /cat/y;
const c = new RegExp("cat", "y");
console.log(a.sticky);
console.log(b.sticky);
console.log(c.sticky);
console.log(c.flags);