Chris Bier
Chris Bier

Reputation: 14447

Regex messes up syntax highlighting

Am I missing something here? This regex is for email validation, and it looks like one of the forward slashes are throwing off the syntax highlighting.

enter image description here

var emailRegex = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;

var validEmail = emailRegex.test(newEmail.email);

Am I doing something wrong? Is there another way to format this regular expression to prevent this from happening?

I was unable to find the answer to this question.

Upvotes: 0

Views: 254

Answers (1)

user2437417
user2437417

Reputation:

You can probably just escape the / in the regex. Otherwise the syntax highlighter thinks it closes the regex literal.

//-----------------------vv
var re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;

Or you could file a bug with whatever editor it is you're using.


Notice that the syntax highlighter on SO doesn't have an issue with the original.

var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;

Upvotes: 3

Related Questions