21

My ESlint throws the error Unexpected control character(s) in regular expression: \x08 no-control-regex for my regular expression let regex = new RegExp("^[0-9a-zA-Z \b]+$");

If i remove \b from my regEx the error is not thrown. Why is this happening? How can i remove the error?

10
  • 1
    probably an option in eslint to ignore this warning, since \b in there is perfectly valid (though, how would you get a string that includes the backspace character?) Apr 10, 2018 at 1:20
  • 1
    I can't reproduce your problem, but assuming backspace is included in general whitespace, then \s should also match backspace. Actually, I'm not sure backspace is an actual character, and it should be noted that \b is a word boundary, not a character. Apr 10, 2018 at 1:27
  • If you need \b you should probably disable this ESLint rule that is set by default eslint.org/docs/rules/no-control-regex
    – BrunoLM
    Apr 10, 2018 at 1:27
  • Not tested but: I think that \b inside a character class is seen as the backspace character (\x08). Apr 10, 2018 at 1:29
  • how would I get a string that includes the backspace character - did you want a backspace in a string? or do you mean how do you get a backspace into a regexp Apr 10, 2018 at 1:36

3 Answers 3

33

The rule no-control-regex is on.

This rule is enabled by default when you inherit rules from eslint:recommended

"extends": "eslint:recommended"

Reason why it's enabled by default:

Control characters are special, invisible characters in the ASCII range 0-31. These characters are rarely used in JavaScript strings so a regular expression containing these characters is most likely a mistake.

To disable this rule, add on your eslint config

"rules": {
  "no-control-regex": 0
}
5
  • 12
    Very interesting, but what a strange rule! (in particular if you consider characters like the tabulation, the carriage return, the newline and eventually the line feed that are very common characters). Apr 10, 2018 at 2:09
  • 9
    "most likely a mistake"... Pfft... Silly.
    – ADJenks
    Feb 20, 2020 at 22:52
  • 2
    I'm using Visual Studio Code with React. I tried editing package.json like this - "eslintConfig": { "extends": "react-app", "rules": { "no-control-regex": 0 } } - but it had no effect.
    – Dror Bar
    May 25, 2020 at 7:02
  • 2
    Hi @DrorBar, try adding above each problematic line: // eslint-disable-next-line
    – Oranit Dar
    Feb 1, 2021 at 7:24
  • 1
    I disagree this is the right answer. Disabling lint rule is like hiding the problem without fixing it. See my answer here for the proper way to fix the error.
    – VCD
    Oct 5, 2021 at 6:12
4

The reasons why ESLint throwing error:

  1. Lint rule no-control-regex is on.
  2. The expression in string is not properly escaped and therefore contains control characters. See the following code:
// This prints: ^[0-9a-zA-Z]+$
// Notice that the space character and \b is missing
console.log("^[0-9a-zA-Z \b]+$")

The right way to fix the error is to escape the string properly.

let regex = new RegExp("^[0-9a-zA-Z \\b]+$");
-2

I got same issue, use \\n to replace \n that's all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.