PDA

View Full Version : Need Help with Regular Expression


demonseed
09-04-2002, 08:50 PM
Hi

I had some form validation problems earlier and the user wap3 wrote this regular expression for me:

/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/

I thought it worked perfectly, however.... I am using IE 5.0 at home (whereas Im using 6.0 at work) and it throws an error for every ? in the expression. removing them makes it work again. Why does this happen?

However, It doesn't allow for an email address like help.me@server.com as it wont allow the . left of the @.

I'm reading the Regular Expression page in the Netscape developer thing, but I can't work it out.

Which bit needs to removed to allow for an email address like the one above?

Many thanks in advance

Matt

mordred
09-04-2002, 09:30 PM
Those (?: indicate a non-capturing pattern. It means that the content of this particular group will not be remembered for further use as backreferences nor will it appear in a matches array.

I believe you stumbled over one of these tiny pecularities of JavaScript (or better, JScript) version history. IIRC these non-capturing statements are available in IE in version 5.5, and that's also the version I tested the RegExp in and it worked ok. Since this RegExp does not employ any backreferences, you can remove these parts, it won't affect functionality (yet slightly performance, but you won't notice).

/^([\w-]+(\.[\w-]+)*)@(([\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(\.[a-z]{2})?)$/

Should also validate an address with a point left of the @-sign.