Disable Unnecessary escape character: \/ no-useless-escape

前端 未结 4 1775
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 15:38

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

相关标签:
4条回答
  • 2021-01-31 15:51
    //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

    0 讨论(0)
  • 2021-01-31 16:00

    \ 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))) { ... }
    
    0 讨论(0)
  • 2021-01-31 16:01

    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 So 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.

    0 讨论(0)
  • 2021-01-31 16:03

    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');
    
    0 讨论(0)
提交回复
热议问题