From 52e100cab31659bf2461db635c42cf25169cc19e Mon Sep 17 00:00:00 2001 From: Chris Wanstrath <2+defunkt@users.noreply.github.com> Date: Tue, 25 Nov 2025 16:00:06 -0800 Subject: [PATCH] regex flags, bad regexs become Words --- src/parser/tokenizer2.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/parser/tokenizer2.ts b/src/parser/tokenizer2.ts index f0a8502..317c580 100644 --- a/src/parser/tokenizer2.ts +++ b/src/parser/tokenizer2.ts @@ -390,7 +390,28 @@ export class Scanner { if (this.char === c`/` && this.peek() === c`/`) { this.next() // skip / this.next() // skip / - this.push(TokenType.Regex) + + // read regex flags + while (this.char > 0 && isIdentStart(this.char)) + this.next() + + // validate regex + const to = this.pos - getCharSize(this.char) + const regexText = this.input.slice(this.start, to) + const [_, pattern, flags] = regexText.match(/^\/\/(.*)\/\/([gimsuy]*)$/) || [] + + if (pattern) { + try { + new RegExp(pattern, flags) + this.push(TokenType.Regex) + break + } catch (e) { + // invalid regex - fall through to Word + } + } + + // invalid regex is treated as Word + this.push(TokenType.Word) break }