admin管理员组

文章数量:1336623

I'm storing some RegExps in an Object as strings, but getting the above error message.

I believe this is because they aren't prefixed with / or suffixed with / - as I'm running them into a new RegExp() constructor, as 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*$'
};

There's the above strings.

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 via new RegExp() surely they'll do this (for example):

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

With double slashes. My question as a bit of a RegExp beginner, is do these double slashes matter? Can it be fixed another way. JSHint is plaining because it's not a "real" RegExp.

I can also remove them from strings and store as true RegExps, but again I need them to be dynamic. Any help appreciated.

I'm storing some RegExps in an Object as strings, but getting the above error message.

I believe this is because they aren't prefixed with / or suffixed with / - as I'm running them into a new RegExp() constructor, as 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*$'
};

There's the above strings.

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 via new RegExp() surely they'll do this (for example):

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

With double slashes. My question as a bit of a RegExp beginner, is do these double slashes matter? Can it be fixed another way. JSHint is plaining because it's not a "real" RegExp.

I can also remove them from strings and store as true RegExps, but again I need them to be dynamic. Any help appreciated.

Share Improve this question asked Apr 22, 2014 at 21:20 Stephen JenkinsStephen Jenkins 1,8363 gold badges26 silver badges40 bronze badges 6
  • 1 It's considerably easier to use native regular expression literals if you don't have dynamic regular expressions (ones that need runtime values in order to be constructed). – Pointy Commented Apr 22, 2014 at 21:26
  • 1 @jfriend00 Please read: "I can also remove them from strings and store as true RegExps, but again I need them to be dynamic". Again, the same question regarding double backslashes would apply. – Stephen Jenkins Commented Apr 22, 2014 at 21:27
  • @Pointy - what if I'm grabbing the regexp from data-regexp="[A-Za-z]{1}" for example. That needs constructing or it's just a string? – Stephen Jenkins Commented Apr 22, 2014 at 21:28
  • 1 If you're stuck with strings, then you're stuck with double escaping. Regex declarations as strings are just messy. I don't know what magic bean you're expecting us to offer you here. If you want a string with "\s" in it, then you have to declare "\\s". – jfriend00 Commented Apr 22, 2014 at 21:29
  • @jfriend00 Yeah - but do they matter or have any negative effects, or will they work like usual? :) – Stephen Jenkins Commented Apr 22, 2014 at 21:30
 |  Show 1 more ment

1 Answer 1

Reset to default 7

The problem is that the backslash character (\) is used both for escaping special characters in string literals (e.g. \n is interpreted as a single newline character, and \\ as a single backslash character), and it's used in escaping special characters in regular expressions.

So when a string literal is used for a regexp, and you need the regexp to see \, you need to escape the backslash and include \\ in the string literal. Specifically, in email you need \\. rather than \.. E.g.

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

Alternatively, you could put the regular expressions in /.../ rather than '...' (or '/.../'). Then string literal escaping doesn't apply, and you don't need to double the slashes. E.g.

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

In the latter case, you also don't need new RegExp(patterns.email), since patterns.email is already a RegExp object.

本文标签: javascriptJSHint quotBad or unnecessary escapingquot Do double slashes beginningend matterStack Overflow