Juanillo
Juanillo

Reputation: 875

Syntax error in Javascript pattern

I'm getting a syntax error in this simple javascript code. I just want to check the atring on a pattern. Can any one tell me what is wrong?

var a = '[email protected]';
var pattern = [a-zA-Z0-9_]+[.[a-zA-Z0-9]+]*@[a-zA-Z0-9_]+[.[a-zA-Z]+]+;
console.log('The comparison is ',a.match(pattern));

Thanks.

Upvotes: 1

Views: 308

Answers (3)

nirmus
nirmus

Reputation: 5103

try

 var pattern = /^[a-zA-Z0-9_]+[.[a-zA-Z0-9]+]*@[a-zA-Z0-9_]+[.[a-zA-Z]+]+$/;

Upvotes: 1

Nivas
Nivas

Reputation: 18354

Try

var pattern = /[a-zA-Z0-9_]+[.[a-zA-Z0-9]+]*@[a-zA-Z0-9_]+[.[a-zA-Z]+]+/;

Patterns are usually delimited by //. See RegExp on MDC.

Upvotes: 4

rdmueller
rdmueller

Reputation: 11042

you have no delimiter on the pattern. Have you tried

var pattern = '[a-zA-Z0-9_]+[.[a-zA-Z0-9]+]*@[a-zA-Z0-9_]+[.[a-zA-Z]+]+';

?

Upvotes: 0

Related Questions