Noushad
Noushad

Reputation: 6781

Javascript Unexpected control character(s) in regular expression

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?

Upvotes: 22

Views: 25820

Answers (3)

Boy.Lee
Boy.Lee

Reputation: 155

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

Upvotes: -2

VCD
VCD

Reputation: 949

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

Upvotes: 7

BrunoLM
BrunoLM

Reputation: 100371

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
}

Upvotes: 37

Related Questions