22

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?) Commented 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. Commented 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 Commented Apr 10, 2018 at 1:27
  • Not tested but: I think that \b inside a character class is seen as the backspace character (\x08). Commented 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 Commented Apr 10, 2018 at 1:36

2 Answers 2

38

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 } 
Sign up to request clarification or add additional context in comments.

5 Comments

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).
"most likely a mistake"... Pfft... Silly.
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.
Hi @DrorBar, try adding above each problematic line: // eslint-disable-next-line
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.
7

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]+$"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.