1

I am getting the following warning in JSLint for my regex expression.

enter image description here

Unexpected '\' before '.'.
var regexForEmail = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

Can someone help me how to fix it or is there any other way to suppress the warning?

Thanks in advance

Dinesh.

Dinesh M
  • 1,026
  • 8
  • 23

1 Answers1

4

. doesn't have special meaning when it's inside square brackets, so there's no need to escape it there. " has no special meaning at all in regular expressions, so you never need to escape it.

var regexForEmail = /^(([^<>()\[\].,;:\s@"]+(\.[^<>()\[\].,;:\s@"]+)*)|(".+"))@(([^<>()\[\].,;:\s@"]+\.)+[^<>()\[\].,;:\s@"]{2,})$/i;

The only characters that are special inside square brackets are backslash, hyphen, right square bracket, and caret at the beginning.

See What special characters must be escaped in regular expressions?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Using your expression still shows me the warning `Expected '\' before '['. var regexForEmail = /^(([^<>()[\].,;:\s@\"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i;` – Dinesh M Mar 31 '17 at 05:36
  • 2
    I'm not sure why JSLint thinks that `[` needs to be escaped inside `[]`, but I've added back those backslashes. I also missed some double quotes that still were escaped, I've fixed them. I just tested this in jslint.com, there are no warnings now. – Barmar Mar 31 '17 at 20:22