regex flags, bad regexs become Words

This commit is contained in:
Chris Wanstrath 2025-11-25 16:00:06 -08:00 committed by Chris Wanstrath
parent 6a6675d30f
commit 0e92525b54

View File

@ -390,7 +390,28 @@ export class Scanner {
if (this.char === c`/` && this.peek() === c`/`) { if (this.char === c`/` && this.peek() === c`/`) {
this.next() // skip / this.next() // skip /
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 break
} }