View Full Version : JavaScript syntax

10-28-2003, 02:11 PM
OK, so I ran across the following code (in someone's webpage) that checks whether an email address submitted is actually valid (in syntax only, my guess).
<!-- Begin
function checkEmail(myForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value)){
return (true)
alert("Invalid E-mail Address! Please re-enter.")
return (false)
// End -->

Could someone go through the line if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value)) end explain the syntax (what checks are made)? For example, what does "/^\w" accomplish?

10-28-2003, 02:40 PM
To a normal person I bet it looks like a lot of BS. :)

10-28-2003, 03:31 PM
Looks like a regular expression. I know they exist, they have form like this and they are a lot used for pattern matching, but that's all I know about them.

Try searching for regular expressions in JavaScript using a search engine. A few sites I've found.

From the first site, / seems to be the start of the expression. Then ^ indicates that the pattern matching should start from the beginning, then \w indicates that any alphanumeric character is valid. So a valid e-mail address should always start with an alphanumeric character.

Jeremy G
10-28-2003, 03:40 PM
Basicly it says
match some ammount of letters, then a @ , then some other letters, then a ., then 3 more letters.

the"[/.w+]" type things are disqualifiers. Making sure there arnt illegal characters lie /, ^, $& or white spaces in the email address.

Zach L.
10-28-2003, 07:11 PM

^ - matches the beginning of the line
\w+ - matches a word character 1 or more times
([\.-]? - matches . or - 0 or 1 times
\w+)* - matches any word character 1 or more times
The * matches the entire expression in parens 0 or more times
@ matches the @ character exactly once
\w+([\.-]?\w+)* - does exactly what it did before
(\.\w{2,3})+ - matches a . followed by 2 to 3 word characters 1 or more times
$ - matches the end of the line

10-28-2003, 07:22 PM
so would that script be basically used for checking if the email adress is the same in two different text fields?

Zach L.
10-28-2003, 08:04 PM
No, it just makes sure that the e-mail address follows a specific format. i.e.:
abc is not valid,
a@b.com is valid,
a-@b.com is not,
a-b@c.com is, etc...

Its just for matching patterns in a string.

10-28-2003, 08:08 PM
why is the matching code so confusing then? I've done javascript before, and actually something quite similar to the above code, but the syntax was a lot different.

Zach L.
10-28-2003, 08:18 PM
You'll find regular expressions in many languages. JavaScript just happens to implement them. The thing is, most other ways of pattern matching are language or library specific. A regular expression is just a string which defines the pattern that you're looking for, and then whatever function gets passed the string (or however it is implemented) has to use its own language/library specific methods to find that pattern. In JavaScript, it appears that regular expressions are in fact objects in themselves. The syntax is a bit odd though (the /reg-exp/.test( ) thing looks a bit strange to me).

10-28-2003, 08:32 PM
I took some time to study the code and your explanation of it, and I guess it does make some logical sense...yet I don't see writting it without a ready reference...even if one coded in JScript all the time, things of that nature would be impossible to remember.

10-28-2003, 08:34 PM
what exactly do regular expressions offer, anyway? does it just save space/time when coding?

Zach L.
10-28-2003, 09:17 PM
Yeah, saving time and space is one thing. Also, they are fairly standard. If you use them from time to time, it can be nice to actually write out the pattern instead of messing with functions and objects.

I certainly can't do too much with them without reference (I can do some basic things, but it always helps to have a table of special characters in front of me). The place I have seen them used most is in Perl code.

10-29-2003, 10:16 AM
Originally posted by Shiro

Try searching for regular expressions in JavaScript using a search engine. A few sites I've found.

...Thanks, that's exactly what I needed. I knew what the code did, just wanted to know what the syntax in the expression meant. Thanks :)