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?
In JavaScript, regular expressions are also objects. These patterns are used with the exec () and test () methods of RegExp, and with the match (), matchAll (), replace (), replaceAll (), search (), and split () methods of String. This chapter describes JavaScript regular expressions. You construct a regular expression in one of two ways:
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. Show activity on this post. Lint rule no-control-regex is on.
The expression in string is not properly escaped and therefore contains control characters. See the following code: The right way to fix the error is to escape the string properly. Thanks for contributing an answer to Stack Overflow!
Control characters are metacharacters, operators, and replacement text characters that can be used in regular expressions. Only half-width characters are recognized. Any full-width characters that correspond to the characters in the following tables are not recognized. Table 1. Regular expression metacharacters Match at the beginning of the input.
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
}
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]+$");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With