Skip to content

Commit 9f18506

Browse files
committed
Add a note about sticky flag usage in regexes
1 parent c51fc7f commit 9f18506

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/core/Tokenizer.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { equalizeWhitespace, escapeRegExp, id } from 'src/utils';
33
import * as regexFactory from './regexFactory';
44
import { type Token, TokenType } from './token';
55

6+
// A note about regular expressions
7+
//
8+
// We're using a sticky flag "y" in all tokenizing regexes.
9+
// This works a bit like ^, anchoring the regex to the start,
10+
// but when ^ anchores the regex to the start of string (or line),
11+
// the sticky flag anchors it to search start position, which we
12+
// can change by setting RegExp.lastIndex.
13+
//
14+
// This allows us to avoid slicing off tokens from the start of input string
15+
// (which we used in the past) and just move the match start position forward,
16+
// which is much more performant on long strings.
17+
618
const WHITESPACE_REGEX = /(\s+)/uy;
719
const NULL_REGEX = /(?!)/uy; // zero-width negative lookahead, matches nothing
820

0 commit comments

Comments
 (0)