/    Sign up×
Community /Pin to ProfileBookmark

Get it? “Reg ex”!!! Okay, maybe you didn’t take Latin in school.

Now that I have your attention…

I’ve read about regex (I purchased “Teach Yourself Regular Expressions in 10 Minutes”, by Ben Forta) and understand what they’re for and why they’re used, but still can’t see the need and what it does in the following javascript (which I copied from another page on this site). It’s a form:

[QUOTE]

function valid(form) {
var field = form.email;
var str = field.value;
if (window.RegExp) {
var reg1str = “(@.*@)|(..)|(@.)|(.@)|(^.)”;
var reg2str = “^.+@([?)[a-zA-Z0-9.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$”;
var reg1 = new RegExp(reg1str);
var reg2 = new RegExp(reg2str);
if (!reg1.test(str) && reg2.test(str))
return true;
field.focus();
field.select();
return false;
} else {
if(str.indexOf(“@”) >= 0)
return true;
field.focus();
field.select();
return false;
}
}

</SCRIPT>
<FORM METHOD=”POST”
ACTION=”mailto:[email protected]
ENCTYPE=”text/plain”
onSubmit=”return valid(this)”>
Your e-mail address:<BR><INPUT TYPE=”text” NAME=”email” SIZE=”40″><BR>
Subject:<BR><INPUT TYPE=”text” NAME=”subject” SIZE=”40″><BR>
Comments:<BR><TEXTAREA NAME=”comments” COLS=”40″ ROWS=”5″></TEXTAREA><BR>
<INPUT TYPE=”submit” VALUE=”Send Mail”>
</FORM>

[/QUOTE]

Also, the book actually leaves out two very important points… ie – where one writes the regex, and how regex highlights the soughtafter text once it does it’s thing.

Any help?
Thanks.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@NietechtNov 08.2007 — A good regular expressions reference is:

http://www.regular-expressions.info/

It's even got a pretty decent page about Javascript implementation:

http://www.regular-expressions.info/javascript.html

The code seems to be a bit redundant...
[CODE]
var reg1str = "(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)";
var reg2str = "^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$";
var reg1 = new RegExp(reg1str);
var reg2 = new RegExp(reg2str);
[/CODE]

can be reduced to simply:
[CODE]
var reg1 = /(@.*@)|(..)|(@.)|(.@)|(^.)/;
var reg2 = /^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$/;
[/CODE]


Basically, because your regexp string is fixed, you can assign it straight to a variable.

A regexp is delimited by //, basically, it's like saying myAr = [] instead of myAr = new Array().

Because it's not a string, you don't need to escape the , which makes it easier to read. You still need to escape the / of course.

If you had to assemble a regexp based on a variable string, then you'd have to use the new RegExp() construct.

To get a matched text returned use the exec method of a regexp:
[CODE]
var matchedstring = /pattern/.match("stringtosearchin");
[/CODE]
Copy linkTweet thisAlerts:
@stickyfingersauthorNov 09.2007 — Huh?

Thanks for responding, but my actual question is: what did the author of the regex hope to find with such a regex? What's he asking?

Thanks again.
Copy linkTweet thisAlerts:
@ZeroKilledNov 09.2007 — i can neither understand too much what exactly the coder want to look for. the first regex don't make sense, it match one of the following in this order:

1- an @ symbol followed by zero or more non-terminal character (like return character) and finally an @ symbol. eg, can match '@@', or '@dknf o/e,f op3iakl3 one@'. so, what exactly means those match? nothing!

2 - two contiguous periods '..'

3 - an @ symbol followed by an period '@.'

4 - a period followed by an @ symbol '.@'

5 - and finally a string that begin with an period.

what i'm understanding from this regex is that coder want to validate an email string. if the regex match any of those possibilities means that the string is invalid. not a clever solution because there are better code and more simplier.

the second regex seem to match email, in a short i will post an image sampling what match each part of the second regex.
Copy linkTweet thisAlerts:
@ZeroKilledNov 09.2007 — in the following attached image you will see two string that match the second regex even if they don't look like an email address. each color represent the part that the pattern match in the string. notice that the red box can match any character. [b]note: the second string is splited in three line for better reading, they do not contain return character or other terminal character.[/b] i would like you to take a look at the [i]note1[/i] and [i]note2[/i]. they represent a possible match by their respective section. but they did not matched because of the pattern [b]^.+[/b] been greedy (greedy, find every possible match). seem that coder did not took too much care, however, the pattern need some fixing for email validation. hope the image help.

note: in the image sample i used the pattern nietecht reduced because is more understandable
×

Success!

Help @stickyfingers spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...