2

I tried this code as provided in https://v2.vuejs.org/v2/cookbook/form-validation.html.

validEmail: function (email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

Vuejs is saying that - error: Unnecessary escape character: \[ (no-useless-escape) at src/components/form.vue:125:65 which is the line var re = ...

What I am getting wrong here? I called this function like this.validEmail(this.modelname)

3
  • So why don't you just remove the backslash? You're inside of a capture group. The only bracket to be escaped is the closing bracket ]. Commented Dec 3, 2018 at 11:21
  • @lher I removed all `` which are before of [ and ], and it comes with other characters to ... Commented Dec 3, 2018 at 11:54
  • Escaping means adding a backslash \ before a character. The bracket [ is escaped by adding a backslash \ character >> \[ Commented Dec 3, 2018 at 12:22

1 Answer 1

3

The "error" (technically it's just a warning) is saying that you do not need to escape [ in the regex when it is inside a character set ([] syntax) because its meaning is unambiguous (you can't create nested character sets); ], on the other hand, does need to be escaped because it will be interpreted as part of the regex syntax that ends the character set instead of a literal ] character.

Simplified example:

/[\[]/
  ^ unnecessary escape

should be instead:

/[[]/
Sign up to request clarification or add additional context in comments.

4 Comments

I tried after removing \[\] and again it said same error message with \. and \- . After all done and no error was there but email validation does not work.
Right, now you're getting similar errors for other unnecessary escapes – remove them too. . does not need to be escaped in a character set. With - I usually put it first after the [ so it needn't be escaped.
I tried that did not work ... error gone but ... validation did not work
You should only be removing the \ character where indicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.