Doreen22 Posted February 18, 2011 Share Posted February 18, 2011 I have seen regular expressions declared a few different ways, in Javascript. Which is best?1) var regex = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);2) var regex=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/ ;3) same as 2, but without the semicolon at the end (my program chokes on that one, but I've seen it on several sites). I am using Javascript in XHTML code (not sure if that makes a difference). Thanks!Doreen Link to comment Share on other sites More sharing options...
jeffman Posted February 18, 2011 Share Posted February 18, 2011 XHTML won't make a difference.1 and 2 both create a regex object. I have the impression that most experienced developers use 2. The use of a semicolon at the end of a statement is optional, but most experienced developers do it. Link to comment Share on other sites More sharing options...
ShadowMage Posted February 18, 2011 Share Posted February 18, 2011 I think the main difference is that you can pass a string in the first technique which then enables the usage of variables to create regexes. Link to comment Share on other sites More sharing options...
jeffman Posted February 18, 2011 Share Posted February 18, 2011 Yes. I forget about that sometimes, but I have used it that way. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.