Skip to content

TS1508: Unexpected '?'. Did you mean to escape it with backslash? shouldn't report even with v flag #62707

Closed
microsoft/typescript-go
#4218

Description

@brettz9

🔎 Search Terms

TS1508, "Did you mean to escape it with backslash?"

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?target=11#code/PQbQ-AZAusBuBQQ

💻 Code

/[?&]/v

🙁 Actual behavior

The error "TS1508: Unexpected '?'. Did you mean to escape it with backslash?" is given.

🙂 Expected behavior

No error because ? doesn't need to be escaped in a character class even with a v flag.

Additional information about the issue

Other characters like { do need escaping with the v flag, but ? gets no runtime errors, so I don't see why this has to err now.

Activity

  1. jcalz commented on Nov 2, 2025

    @jcalz
    Contributor

    This changed between versions 5.6.3 and 5.7.2

    No, it was present in 5.6.3. Maybe you should edit it to "This changed in #55600 which introduced regular expression syntax checking"


    A strange thing about this error is that it mentions ? but it is really about & (that's where the error squiggly is). If you escape the ? the error persists, and only goes away if you escape the &. Indeed it looks like the error talks about the first character (e.g., if you write /[\?&]/v to "fix" the error, the error stays but now it says "unexpected \"). So whether or not there's a bug around requiring & to be escaped (I think only && needs to be escaped, right?) there does seem to be a bug around the text of the message... so this is... two bug reports? Should we file another one (if neither are duplicates)?

  2. brettz9 commented on Nov 2, 2025

    @brettz9
    Author

    This changed between versions 5.6.3 and 5.7.2

    No, it was present in 5.6.3. Maybe you should edit it to "This changed in #55600 which introduced regular expression syntax checking"

    I did check, but must have messed it up. Thanks!

    A strange thing about this error is that it mentions ? but it is really about & (that's where the error squiggly is). If you escape the ? the error persists, and only goes away if you escape the &. Indeed it looks like the error talks about the first character (e.g., if you write /[\?&]/v to "fix" the error, the error stays but now it says "unexpected \"). So whether or not there's a bug around requiring & to be escaped (I think only && needs to be escaped, right?)

    Right. /[?&&]/v gives an error in Chrome, for example, while /[?&]/v does not.

  3. RyanCavanaugh commented on Nov 3, 2025

    @RyanCavanaugh
    Member
  4. Andarist commented on Nov 3, 2025

    @Andarist
    Contributor

    The incorrectly reported character gets fixed by this patch:

    diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts
    index 343df39ad9..3619f57180 100644
    --- a/src/compiler/scanner.ts
    +++ b/src/compiler/scanner.ts
    @@ -3103,7 +3103,7 @@ export function createScanner(
                         operand = scanClassSetOperand();
                         break;
                 }
    -            switch (charCodeChecked(pos)) {
    +            switch (ch = charCodeChecked(pos)) {
                     case CharacterCodes.minus:
                         if (charCodeChecked(pos + 1) === CharacterCodes.minus) {
                             if (isCharacterComplement && mayContainStrings) {

    But the other part of the issue would have to be investigated further.

  5. graphemecluster commented on Nov 4, 2025

    @graphemecluster
    Contributor

    These 3 lines shouldn't have been there in the first place 😄

    diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts
    index 343df39ad9..986a3dcf5a 100644
    --- a/src/compiler/scanner.ts
    +++ b/src/compiler/scanner.ts
    @@ -3125,9 +3125,6 @@ export function createScanner(
                             mayContainStrings = !isCharacterComplement && expressionMayContainStrings;
                             return;
                         }
    -                    else {
    -                        error(Diagnostics.Unexpected_0_Did_you_mean_to_escape_it_with_backslash, pos, 1, String.fromCharCode(ch));
    -                    }
                         break;
                     default:
                         if (isCharacterComplement && mayContainStrings) {
  6. Ijtihed commented on Jun 5, 2026

    @Ijtihed
    Contributor

    I fixed lone & handling in /v character classes and added a compiler regression test for it asw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: ParserThe parser didn't handle the syntax correctlyHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions