HighDots Forums  

Regular Expressions anyone?

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Regular Expressions anyone? in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
FunGuySF
 
Posts: n/a

Default Regular Expressions anyone? - 01-08-2005 , 12:29 AM






I'm baffled! I've been reading, for days, about how to create regular
expressions.... and I'm lost. Can someone help me create RE's to do the
following?

1) Test a "Username" string. The string can contain 2 to 15 word
characters and hyphens but nothing else. I figured out /^\w{2,15}$/, but
how to I tell it hyphens are cool as well? I tried /^-?\w{2,15}-?$/, which
works if a hyphen starts or ends the string, but how can I tell it that
hyphens are okay anywhere in the middle, and still limit the total string
length to {2,15}?

2) If it fails, I'm telling the user that the username is invalid, and am
rejecting spaces embedded in the string (e.g., "user name" = false).
However, I'd rather just trim whitespace if it falls at the start or end of
the string before I test it, without bothering the user (e.g., trim("
username ") = "username", which would then test = true), but leave alone
whitespace in the middle (e.g., " user name " = "user name", which would
then test = false because of the space in the middle).

Thanks for your help!

Mike



Reply With Quote
  #2  
Old   
Ociretsih Yahoo
 
Posts: n/a

Default Re: Regular Expressions anyone? - 01-08-2005 , 06:08 AM







"FunGuySF" <funguysfATpsmonopolyDOTorg> escribió en el mensaje
news:3oGdnbOZQrww8kLcRVn-3g (AT) comcast (DOT) com...
Quote:
I'm baffled! I've been reading, for days, about how to create regular
expressions.... and I'm lost. Can someone help me create RE's to do the
following?

1) Test a "Username" string. The string can contain 2 to 15 word
characters and hyphens but nothing else. I figured out /^\w{2,15}$/, but
how to I tell it hyphens are cool as well? I tried /^-?\w{2,15}-?$/,
which works if a hyphen starts or ends the string, but how can I tell it
that hyphens are okay anywhere in the middle, and still limit the total
string length to {2,15}?

Try this one-line:
Quote:
perl -e "$_=qq(5peter-john-l);print qq(MATCH) if (/^[\w-]{2,15}$/)"
MATCH

Quote:
2) If it fails, I'm telling the user that the username is invalid, and am
rejecting spaces embedded in the string (e.g., "user name" = false).
However, I'd rather just trim whitespace if it falls at the start or end
of the string before I test it, without bothering the user (e.g., trim("
username ") = "username", which would then test = true), but leave alone
whitespace in the middle (e.g., " user name " = "user name", which would
then test = false because of the space in the middle).

Thanks for your help!

Mike

If you have Perl properly installed you can use the online help:
Quote:
perldoc -q white
that gives you
$string =~ s/^\s+//; #trim starting white space
$string =~ s/\s+$//; #trim ending white space

Good luck! (Use the help, please!)




Reply With Quote
  #3  
Old   
Ociretsih Yahoo
 
Posts: n/a

Default Re: Regular Expressions anyone? - 01-08-2005 , 06:11 AM




"Ociretsih Yahoo" <ociretsih (AT) yahoo (DOT) com> escribió en el mensaje
news:waPDd.45716$yo5.42333 (AT) twister (DOT) auna.com...

SORRY! A was thinking it was a Perl post!



Reply With Quote
  #4  
Old   
Ociretsih Yahoo
 
Posts: n/a

Default Re: Regular Expressions anyone? - 01-08-2005 , 06:54 AM



SORRY again! I think this will fix my first wrong post
=====================================

I've seen a couple of web sites stating that:
---> The syntax of JavaScript RegEx's is the same as of Perl's
So, a little rewriting of my previous answer will suffice:

/*== JavaScript program ==*/
var myRE = /^[\w-]{2,15}$/;
var myString = "5peter-johnny-l";
var theResult;

if (myString.match(myRE))
{
theResult = "MATCHED";
} else
{
theResult = "UNMATCHED";
}

WScript.Echo(theResult); /* WARNING! This line is Microsoft's JScript
"standard output" - just delete it */
/*== End of JavaScript program ==*/



Reply With Quote
  #5  
Old   
FunGuySF
 
Posts: n/a

Default Re: Regular Expressions anyone? - 01-09-2005 , 10:09 PM



Thanks! That worked like a champ. I now understand how the []'s work.

I did figure out how to take care of the starting and trailing whitespace as
well, from your perl example.

Mike


"Ociretsih Yahoo" <ociretsih (AT) yahoo (DOT) com> wrote

Quote:
SORRY again! I think this will fix my first wrong post
=====================================

I've seen a couple of web sites stating that:
---> The syntax of JavaScript RegEx's is the same as of Perl's
So, a little rewriting of my previous answer will suffice:

/*== JavaScript program ==*/
var myRE = /^[\w-]{2,15}$/;
var myString = "5peter-johnny-l";
var theResult;

if (myString.match(myRE))
{
theResult = "MATCHED";
} else
{
theResult = "UNMATCHED";
}

WScript.Echo(theResult); /* WARNING! This line is Microsoft's JScript
"standard output" - just delete it */
/*== End of JavaScript program ==*/





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.