Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 733
Noushad Avatar asked Apr 10 '18 01:04

Noushad


People also ask

What is a regular expression in JavaScript?

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:

What are 0-31 control characters in JavaScript?

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.

How to fix string expression does not contain control characters?

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!

What are control characters in access?

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.


2 Answers

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
}
like image 160
BrunoLM Avatar answered Sep 24 '22 21:09

BrunoLM


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]+$");
like image 27
VCD Avatar answered Sep 22 '22 21:09

VCD