Regular expressions and text guides
Regex state: global, sticky and lastIndex
Explain repeated test results and parse contiguous tokens without skipping invalid gaps.
Recognize a stateful regex
A regex with g or y updates lastIndex after successful matching. Reusing the same object can start the next test later in the string.
If you want a stateless membership test, omit those flags or reset lastIndex deliberately before each test.
const r=/a/g;
r.test('a') → true
r.test('a') → false
r.lastIndex → 0Use sticky for contiguous parsing
Global search can scan ahead to find the next match. Sticky requires a match exactly at lastIndex and fails if the next input is invalid.
For tokenization, advance through the expected separators explicitly instead of letting a global search silently skip a gap.
const r=/\d+/y; r.lastIndex=2;
r.exec('x 42')?.[0] → '42'
r.lastIndex=1; r.exec('x 42') → nullHandle zero-length matches
Some patterns successfully match an empty string. In a hand-written exec loop, that can leave lastIndex unchanged and repeat forever.
Use a suitable iterator or explicitly advance by a code point when a match is empty. Unicode mode affects correct advancement.
Distinguish a run from persistent state
The site creates a regex for each run, so repeated button clicks are not a demonstration of reusing one application regex object.
Reproduce the state fixture in your actual code, then use Regex Tester to check the pattern and inputs independently.