JSHint "Bad or Unnecessary Escape." Do double start / end slashes?

I save some RegExps in the object as strings, but I get the above error message.

I believe this is due to the fact that they do not have a prefix / or have a suffix //, since I run them in the new RegExp () constructor, since the script allows users to define RegExps, so I want them all to be dynamic.

var patterns = {
    email: '^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$',
    url: '[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)',
    number: '^[-+]?[0-9]*\.?[0-9]+$',
    empty: '^\\s*$'
};

Here are the lines above.

To fix them, I can do this and // them:

var patterns = {
    email: '/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
    url: '/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/',
    number: '/^[-+]?[0-9]*\.?[0-9]+$',
    empty: '/^\\s*$/'
};

But when called through new RegExp(), of course, they will do it (for example):

var reg = new RegExp(patterns.empty);
/**
 * reg = //^\\s*$//
 */

With a double slash. My question, as a newbie to RegExp, is these double dictionaries? This can be fixed in a different way. JSHint complains because it is not a "real" RegExp.

true RegExps, , . .

+4
1

, (\) (, \n , \\ ) .

, , , regexp \, \\ . , email \\., \.. .

email: '^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$'

/.../, '...' ( '/.../'). , . .

email: /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

new RegExp(patterns.email), patterns.email RegExp.

+7

Source: https://habr.com/ru/post/1537579/


All Articles