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
> 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/>
> laredotorn...@zipmail.com wrote:
>> 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
> 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
> Thomas 'PointedEars' Lahn wrote:
>> Morgan wrote:
>> > [...]
>> > <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
>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.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.