68

I have this regex of mine that will check the string if it contains link or url (i.e. https://eslint.org/docs/rules/no-useless-escape). Using this regex /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, I've encountered and error while running my test cases in react about Unnecessary escape character: \/ no-useless-escape. How to disable this eslint-error in order for me to proceed with my test case and use the regex.

Appreciate for any help!

Rherroral
  • 705
  • 1
  • 5
  • 9

8 Answers8

94

You can use ESLint and try adding either of the things:-

  1. //eslint-disable-line on the line to disable warnings.
  2. //eslint-disable-next-line to line before to disable warnings.

See from docs of ESLint, Disabling Rules with Inline Comments.

To disable all rules on a specific line, use a line or block comment in one of the following formats:

alert('foo'); // eslint-disable-line

// eslint-disable-next-line
alert('foo');

/* eslint-disable-next-line */
alert('foo');

alert('foo'); /* eslint-disable-line */

You can disable warnings in entire file by adding /* eslint-disable */ at the top of the file.

To disable rule warnings in an entire file, put a /* eslint-disable */ block comment at the top of the file:

/* eslint-disable */
   alert('foo');
Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43
61

It's the \/ in [-A-Z0-9+&@#\/%?=~_|!:,.;] and [-A-Z0-9+&@#\/%=~_|] (NOT the ones in :\/\/). Most characters do not have to be escaped within a character class (square brackets). This should be equivalent: /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/ig See https://www.regular-expressions.info/charclass.html for more info, but the relevant part:

In most regex flavors, the only special characters or metacharacters inside a character class are the closing bracket ], the backslash , the caret ^, and the hyphen -. The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.

Eric Haynes
  • 5,126
  • 2
  • 29
  • 36
  • 6
    How can I disable it? – C Williams Oct 10 '18 at 10:04
  • 3
    If you're using `create-react-app`, you probably can't without ejecting, but it's a valid warning, so easiest way to "disable" it is to fix it. ;-) – Eric Haynes Jan 20 '19 at 01:41
  • 1
    For the record you actually *can* override these in React using the special ESLint comments in Abhinav Kinagi's answer. – kontextify Mar 29 '20 at 11:59
  • 1
    But if I paste `[:.,/]` into https://regex101.com/ it says there's a pattern error - "An unescaped delimiter must be escaped with a backslash (\\)" Shouldn't this be okay? – evolross May 19 '22 at 20:47
  • 1
    @evolross On the left, there is a selector for which regex implementation to use. Looks like that error is only reported for the 2 PHP flavors, not the others. Unfortunately, there is no single standard between implementations, so there will be some variances between implementations. There are a lot more similarities than differences, so it's a lot more portable than e.g. programming language syntax. Still, if using a tool like that site, it's generally best to select one that uses the engine from the language you're using. – Eric Haynes May 19 '22 at 22:30
24

You can also use

/* eslint-disable no-useless-escape */

to disable rule for entire script file.

efirat
  • 3,679
  • 2
  • 39
  • 43
12
//eslint-disable-next-line

place this above the line of code

OR

/*eslint no-undef: 0*/

place this on the first line of the file(or first line of the script tag) this would make eslint disabled on the whole file

Daniel Adegoke
  • 179
  • 1
  • 4
4

\ gives error from below code in my NodeJS typescript project, code editor is VS Code -

Code -

if (!(/^[\-0-9a-zA-Z\.\+_]+@[\-0-9a-zA-Z\.\+_]+\.[a-zA-Z]{2,}$/).test(String(req.body.email))) { ... }

Error -

Unnecessary escape character: \+. (eslintno-useless-escape)

Solution -

//eslint-disable-next-line

Final code -

//eslint-disable-next-line
if (!(/^[\-0-9a-zA-Z\.\+_]+@[\-0-9a-zA-Z\.\+_]+\.[a-zA-Z]{2,}$/).test(String(req.body.email))) { ... }
Pinaki
  • 792
  • 8
  • 17
1

I had the following code snippet for validating an email address:

/[a-zA-Z0-9\.]*@[a-z]*[\.a-z]*/.test(value)

The linter was showing an error due to '\.' inside square brackets ('[]'). Square brackets do not need an escape character ('\') to make use of '.'. I removed the '\' inside '[]' (as shown below) and the error got resolved.

/[a-zA-Z0-9.]*@[a-z]*[.a-z]*/.test(value)
1

I just have to remove the

\

from my regx and here how it looked before

/^\w+\-/g;

after

/^\w+-/g;
Divakar R
  • 773
  • 1
  • 8
  • 36
-1

I had a similar warning in react.

Unnecessary escape character: \# no-useless-escape

Simply just remove whatever is mentioned in the warning. In my case, I had \# in the warning, so I just removed it from the line which was mentioned in the warning.

Sanan Ali
  • 2,349
  • 1
  • 24
  • 34