JavaScript RegExp sticky Property

Beginner
5 min read
Updated: Nov 2026
3 examples
RegExp

Definition

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 at lastIndex. 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
CallWhat it does
/cat/ySticky matching. sticky is true.
re.lastIndex = nChoose 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

NameDescription
regex.stickyRead-only. true if the y flag was set when the RegExp was created.
y flagSet at creation with /…/y or new RegExp(pattern, "y").
lastIndexThe exact index where the sticky match must begin.

Return Value

ValueDescription
trueThe RegExp has the y (sticky) flag.
falseThe 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);
Try It Yourself

How It Works

  1. Without y, sticky is false.
  2. With /cat/y, sticky is true.
  3. The constructor with "y" does the same.
  4. flags reports "y".

Example 2 — Match only at lastIndex

Set the index, then call exec.

JavaScript
const re = /cat/y;
const text = "the cat";

re.lastIndex = 0;
console.log(re.exec(text));

re.lastIndex = 4;
console.log(re.exec(text)[0]);
console.log(re.lastIndex);
Try It Yourself

How It Works

  1. At index 0 the text starts with the, so sticky matching fails.
  2. At index 4 the letters cat begin, so the match succeeds.
  3. After a match, lastIndex moves past the match (to 7).

Example 3 — Sticky vs global

Global can search ahead; sticky cannot.

JavaScript
const text = "xx42yy";
const g = /\d+/g;
const y = /\d+/y;

g.lastIndex = 0;
y.lastIndex = 0;

console.log(g.exec(text)[0]);
console.log(y.exec(text));
Try It Yourself

How It Works

  1. Global finds 42 even though digits are not at index 0.
  2. Sticky requires a match at index 0, so it returns null.
  3. Use sticky when your parser already knows the next token position.

Frequently Asked Questions

It is true when the regex was created with the y flag. Matches must start exactly at lastIndex.
Global searches forward from lastIndex and can skip characters. Sticky must match at lastIndex with no skipping.
Yes, for sticky matching. Set regex.lastIndex to the position you want to test, then call exec() or test().
Yes, but they are different behaviors. Most sticky use cases use y alone.
lastIndex advances to the end of the match, so the next sticky call continues from there.
No. Create a new RegExp with the y flag.

Next: unicode

Treat the pattern as Unicode code points with the u flag.

unicode tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful