Reputation: 6781
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
Reputation: 949
The reasons why ESLint throwing error:
// 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
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