newbie question about regular expressions

L

laredotornado

Hello,

I'm just starting out learning regular expressions. How would I write
regular expression that checks if a variable only contains letters,
numbers, and/or underscores? I would want to throw an alert message if
the variable did not satisfy this condition.

Thanks for your help, - Dave
 
T

Thomas 'PointedEars' Lahn

I'm just starting out learning regular expressions. How would I write
regular expression that checks if a variable only contains letters,
numbers, and/or underscores? I would want to throw an alert message
if the variable did not satisfy this condition.

var s = "...";

if (/[^\w]/.test(s))
{
alert("D'oh!");
}

RTFFAQ.[1] RTFM. STFW.


PointedEars
___________
[1] <http://jibbering.com/faq/>
 
M

Morgan

Hello,

I'm just starting out learning regular expressions. How would I write
regular expression that checks if a variable only contains letters,
numbers, and/or underscores? I would want to throw an alert message if
the variable did not satisfy this condition.

Thanks for your help, - Dave

Wow, finally a quizzie I can do! :)

(preens self in mirror next to computer and then begins to type)

<script type="text/javascript">
<!--

var myVar = "This is the variable to check"
var pattern = /\w/ // \w means a-z, A-Z, 0-9 and _

if(pattern.test(myVar))
{
//do your action here
}
else
{
//throw alert
alert("You stupid user!");
}

//-->
</script>

Hope that helps.
 
T

Thomas 'PointedEars' Lahn

Morgan said:
I'm just starting out learning regular expressions. How would I write
regular expression that checks if a variable only contains letters,
numbers, and/or underscores? I would want to throw an alert message if
the variable did not satisfy this condition.
[...]

Wow, finally a quizzie I can do! :)

Sorry, no :)
[...]
<script type="text/javascript">
<!--

`script' element's content should not be tried to comment out like this.
var myVar = "This is the variable to check"
var pattern = /\w/ // \w means a-z, A-Z, 0-9 and _

if(pattern.test(myVar))
{
//do your action here
}
else
{
//throw alert
alert("You stupid user!");
}

This will let e.g. "-a" pass (since "a" matches \w) although it does not
satisfy the condition set by the OP (_all_ characters of the string must
apply to it). You need to reverse the condition, and the pattern like I
did.

var pattern = /[^\w]/; // \w means a-z, A-Z, 0-9 and _

if (!pattern.test(myVar)) // no "bad" characters
{
//do your action here
}
else
{
//throw alert
alert("You stupid user!");
}


PointedEars
 
L

Luke Matuszewski

Thomas 'PointedEars' Lahn napisal(a):
This will let e.g. "-a" pass (since "a" matches \w) although it does not
satisfy the condition set by the OP (_all_ characters of the string must
apply to it). You need to reverse the condition, and the pattern like I
did.

var pattern = /[^\w]/; // \w means a-z, A-Z, 0-9 and _

Shouldn't it be smth like this:
var pattern = /[^\w]*/; // \w means a-z, A-Z, 0-9 and _ zero or more
times or

var pattern = /[^\w]+/; // \w means a-z, A-Z, 0-9 and _ one or more
times (so var could not be empty).
 
T

Thomas 'PointedEars' Lahn

Luke said:
Thomas 'PointedEars' Lahn napisal(a):
This will let e.g. "-a" pass (since "a" matches \w) although it does not
satisfy the condition set by the OP (_all_ characters of the string must
apply to it). You need to reverse the condition, and the pattern like I
did.

var pattern = /[^\w]/; // \w means a-z, A-Z, 0-9 and _

Shouldn't it be smth like this:
var pattern = /[^\w]*/; // \w means a-z, A-Z, 0-9 and _ zero or more
times or

var pattern = /[^\w]+/; // \w means a-z, A-Z, 0-9 and _ one or more
times (so var could not be empty).

No, it should not. If the string contains one non-word character then
it already no longer qualifies as "only contains letters, numbers, and/or
underscores". Due to greedy matching, your versions will only potentially
decrease the efficiency for detecting this.

However, it just came to my mind that [^\w] should be expressed as \W in
all cases.

What is still debatable is that my and your first expression will let pass
the empty string. I think the condition provided by the OP (see above)
is not clear enough to determine whether or not that would be desired
behavior.


PointedEars
 
M

Morgan

Thomas said:
Morgan said:
I'm just starting out learning regular expressions. How would I write
regular expression that checks if a variable only contains letters,
numbers, and/or underscores? I would want to throw an alert message if
the variable did not satisfy this condition.
[...]

Wow, finally a quizzie I can do! :)

Sorry, no :)
[...]
<script type="text/javascript">
<!--

`script' element's content should not be tried to comment out like this.

Really? I was told to include html comments in script tags always
because browsers detect html comments in script tags and check them for
javascript, its a form of browser detection for the browsers which
don't support javascript, they would output the script to the webpage
as text otherwise.

http://www.javascriptref.com/chapters/ch01.htm

The book I'm using has a free chapter which explained this to me, if
you download chapter 1, check under the head "Script Hiding", it
explains it better than I could. Or am I completely wrong?
var myVar = "This is the variable to check"
var pattern = /\w/ // \w means a-z, A-Z, 0-9 and _

if(pattern.test(myVar))
{
//do your action here
}
else
{
//throw alert
alert("You stupid user!");
}

This will let e.g. "-a" pass (since "a" matches \w) although it does not
satisfy the condition set by the OP (_all_ characters of the string must
apply to it). You need to reverse the condition, and the pattern like I
did.

var pattern = /[^\w]/; // \w means a-z, A-Z, 0-9 and _

if (!pattern.test(myVar)) // no "bad" characters
{
//do your action here
}
else
{
//throw alert
alert("You stupid user!");
}


PointedEars

Ah, good advice :) Thanks.
 
T

Thomas 'PointedEars' Lahn

Morgan said:
Thomas said:
Morgan said:
[...]
<script type="text/javascript">
<!--

`script' element's content should not be tried to comment out like this.

Really? I was told to include html comments in script tags always
because browsers detect html comments in script tags and check them for
javascript, its a form of browser detection for the browsers which
don't support javascript, they would output the script to the webpage
as text otherwise.

http://www.javascriptref.com/chapters/ch01.htm

Your reference is outdated. The `script' element was introduced along with
the `style' element in HTML 3.2 (1997). Versions of HTML prior to that
have been obsoleted by RFC2854 (June 2000), so there is no need for trying
to hide anything. No conforming user agent will parse this content as
PCDATA, especially it must not be displayed if found within the `head'
element. And in contrast of such "comments" within CSS, they have never
been specified for JS/ECMAScript, hence using them in such script code is
potentially harmful; this includes, but is not restricted to, use in XHTML
(where an XML parser is allowed to remove all markup comments before
building the parse tree).

See previous discussions on the subject, and please trim your quotes.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 19 Nov 2005 13:37:02, seen in (e-mail address removed) posted :
I'm just starting out learning regular expressions. How would I write
regular expression that checks if a variable only contains letters,
numbers, and/or underscores? I would want to throw an alert message if
the variable did not satisfy this condition.

<URL:http://www.merlyn.demon.co.uk/js-index.htm#RE>; \W

Ignore any answers with /^ or $/ or \w ; there's no need to check the
whole of a failing string, since a single instance of a non-allowed
character is decisive. There's no need to use [^\w] either, as should
be obvious.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top