Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22458 > unrolled thread

Regular expression error in hasClass()?

Started by"Robert Crandal" <noreply2me@yahoo.com>
First post2013-12-28 16:20 -0700
Last post2013-12-31 10:43 -0500
Articles 10 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  Regular expression error in hasClass()? "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-28 16:20 -0700
    Re: Regular expression error in hasClass()? Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2013-12-29 00:49 +0100
      Re: Regular expression error in hasClass()? "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-28 17:20 -0700
        Re: Regular expression error in hasClass()? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-28 23:43 -0200
          Re: Regular expression error in hasClass()? "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-28 19:44 -0700
            Re: Regular expression error in hasClass()? Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2013-12-29 12:16 +0100
            Re: Regular expression error in hasClass()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-29 17:45 +0100
              Re: Regular expression error in hasClass()? "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-29 14:29 -0700
                Re: Regular expression error in hasClass()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-30 10:33 +0100
            Re: Regular expression error in hasClass()? williamc <temp4@williamc.com> - 2013-12-31 10:43 -0500

#22458 — Regular expression error in hasClass()?

From"Robert Crandal" <noreply2me@yahoo.com>
Date2013-12-28 16:20 -0700
SubjectRegular expression error in hasClass()?
Message-ID<lsmdnVyNOOxSwSLPnZ2dnUVZ5j-dnZ2d@giganews.com>
In Chapter 5 of "Pro JavaScript Techniques" (C) 2006, the
book defines a hasClass() function that is supposed to search
for all elements that have a particular class name.  Here is the
code (minus comments) that is printed verbatim in the book:

function hasClass(name,type) {
  var r = [];
  var re = new RegExp("(^|\\s)" + name + "(\\s|$)");
  var e = document.getElementsByTagName(type || "*");
  for (var j = 0; j < e.length; j++)
    if (re.test(e[j])) r.push(e[j]);
  return r;
}

Is there an error in the above code?  It doesn't seem to
work.

Here is the full HTML test case that I created:


<!DOCTYPE html><html><head>
<title>hasClass() test</title>
<meta charset="utf-8">
<script>
function hasClass(name,type) {
  var r = [];
  var re = new RegExp("(^|\\s)" + name + "(\\s|$)");
  var e = document.getElementsByTagName(type || "*");
  for (var j = 0; j < e.length; j++)
    if (re.test(e[j])) r.push(e[j]);   /* Fails here? */
  return r;
}
</script>
</head>
<body>

<div class="foo">Hello</div>
<div class="foo">World</div>

<script>
var x = hasClass("foo", "div");
alert("Number of foo div's found: " + x.length);
</script>
</body></html>

Does anyone see the problem?



[toc] | [next] | [standalone]


#22461

FromBjoern Hoehrmann <bjoern@hoehrmann.de>
Date2013-12-29 00:49 +0100
Message-ID<beoub9d5qv59mq0571grds7ifo031cvfup@hive.bjoern.hoehrmann.de>
In reply to#22458
* Robert Crandal wrote in comp.lang.javascript:
>In Chapter 5 of "Pro JavaScript Techniques" (C) 2006, the
>book defines a hasClass() function that is supposed to search
>for all elements that have a particular class name.  Here is the
>code (minus comments) that is printed verbatim in the book:
>
>function hasClass(name,type) {
>  var r = [];
>  var re = new RegExp("(^|\\s)" + name + "(\\s|$)");
>  var e = document.getElementsByTagName(type || "*");
>  for (var j = 0; j < e.length; j++)
>    if (re.test(e[j])) r.push(e[j]);
>  return r;
>}

This does not access the class names for an element. It has to be

  ...
  if (re.test(e[j].className)) r.push(e[j])
  ...

There are probably other errors, for instance, `\s` matches characters
that do not separate class names, and `name` might contain characters
that have a special meaning in regular expressions like `.` that need
to be escaped.
-- 
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ 

[toc] | [prev] | [next] | [standalone]


#22463

From"Robert Crandal" <noreply2me@yahoo.com>
Date2013-12-28 17:20 -0700
Message-ID<mtydnWvCfJ9S9yLPnZ2dnUVZ5tOdnZ2d@giganews.com>
In reply to#22461
"Bjoern Hoehrmann" wrote:
>
> This does not access the class names for an element. It has to be
>
>  if (re.test(e[j].className)) r.push(e[j])
>
> There are probably other errors, for instance, `\s` matches characters
> that do not separate class names, and `name` might contain characters
> that have a special meaning in regular expressions like `.` that need
> to be escaped.
>

That was it!  I thought it might be missing "className" somewhere in
the hasClass() function.

I found another error in this book later in Chapter 9 where he tries
to call an undefined function named "byClass()".   I think the author was
refering to the previously defined "hasClass()" function.

Oh, in the "Acknowledgements" section of this book, it also says:

   "I'd also like to thank my technical editor, Dan Webb, for thoroughly
    checking my code and reminding me of the finer points of the
    JavaScript language."

I guess his thoroughness wasn't good enough, haha.

Robert

[toc] | [prev] | [next] | [standalone]


#22465

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2013-12-28 23:43 -0200
Message-ID<l9nune$9kc$1@speranza.aioe.org>
In reply to#22463
On 28/12/2013 22:20, Robert Crandal wrote:
> "Bjoern Hoehrmann" wrote:
>>
>> This does not access the class names for an element. It has to be
>>
>>   if (re.test(e[j].className)) r.push(e[j])
>>
>> There are probably other errors, for instance, `\s` matches characters
>> that do not separate class names, and `name` might contain characters
>> that have a special meaning in regular expressions like `.` that need
>> to be escaped.
>>
>
> That was it!  I thought it might be missing "className" somewhere in
> the hasClass() function.
>
> I found another error in this book later in Chapter 9 where he tries
> to call an undefined function named "byClass()".   I think the author was
> refering to the previously defined "hasClass()" function.
>
> Oh, in the "Acknowledgements" section of this book, it also says:
>
>     "I'd also like to thank my technical editor, Dan Webb, for thoroughly
>      checking my code and reminding me of the finer points of the
>      JavaScript language."
>
> I guess his thoroughness wasn't good enough, haha.
>

Is John Resig the author of JS Pro Techniques? If so, you might dismiss 
the book.

A better written code by David Mark, author of "My Library":
<http://www.cinsoft.net/mylib.html>

function hasClassName (el, className) {
   return (new RegExp('(^|\\s)' + className +
     '(\\s|$)')).test(el.className);
}


-- 
Joao Rodrigues

[toc] | [prev] | [next] | [standalone]


#22466

From"Robert Crandal" <noreply2me@yahoo.com>
Date2013-12-28 19:44 -0700
Message-ID<8JCdnVAvR4g3ESLPnZ2dnUVZ5vCdnZ2d@giganews.com>
In reply to#22465
"Joao Rodrigues" wrote:
>
> Is John Resig the author of JS Pro Techniques? If so, you might dismiss 
> the book.
>

Yes, it has John Resig's name on the front cover.

I take it that not many people here are fans of John Resig or his books
or jQuery code or whatever?



[toc] | [prev] | [next] | [standalone]


#22472

FromHans-Georg Michna <hans-georgNoEmailPlease@michna.com>
Date2013-12-29 12:16 +0100
Message-ID<a310c95m16fo3465t40cj99u13gpoljs88@4ax.com>
In reply to#22466
On Sat, 28 Dec 2013 19:44:57 -0700, Robert Crandal wrote:

>I take it that not many people here are fans of John Resig or his books
>or jQuery code or whatever?

Safe assumption. (:-)

Hans-Georg

[toc] | [prev] | [next] | [standalone]


#22476

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-12-29 17:45 +0100
Message-ID<2494021.XN8l84tumM@PointedEars.de>
In reply to#22466
Robert Crandal wrote:

> "Joao Rodrigues" wrote:
>> Is John Resig the author of JS Pro Techniques? If so, you might dismiss
>> the book.
> 
> Yes, it has John Resig's name on the front cover.
> 
> I take it that not many people here are fans of John Resig or his books
> or jQuery code or whatever?

He is a wannabe and a book author.  Does this answer your question?

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#22482

From"Robert Crandal" <noreply2me@yahoo.com>
Date2013-12-29 14:29 -0700
Message-ID<Qb2dnQ_yZoXdCV3PnZ2dnUVZ5vSdnZ2d@giganews.com>
In reply to#22476
"Thomas 'PointedEars' Lahn" wrote:
>>
>> I take it that not many people here are fans of John Resig or his books
>> or jQuery code or whatever?
>
> He is a wannabe and a book author.  Does this answer your question?
>

Yes, I'm realizing that a few on this group are not big fans, and that's
okay.  Everyone is entitled to their opinion.

As for jQuery, I gotta say that my first impression was overall good.
I like the idea of having one module that tries to smooth over the 
differences
between browsers; however, I did find a few bugs in my initial testing.  For
example, this code fails in IE5, IE6, IE7, IE8 (and maybe IE9-11):

$("html").html("<head><title>New title</title></head><body>New 
body</body>");

So, ya, I'm slowly realizing that Resig isn't perfect, but he's okay in my
book.

Robert


[toc] | [prev] | [next] | [standalone]


#22489

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-12-30 10:33 +0100
Message-ID<2441749.WdfFJYrd8m@PointedEars.de>
In reply to#22482
Robert Crandal wrote:

> […]
> So, ya, I'm slowly realizing that Resig isn't perfect, […]

There is hope for you yet.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#22496

Fromwilliamc <temp4@williamc.com>
Date2013-12-31 10:43 -0500
Message-ID<iN2dnb_mYYmEe1_PnZ2dnUVZ_rqdnZ2d@giganews.com>
In reply to#22466
On 12/28/2013 9:44 PM, Robert Crandal wrote:
> "Joao Rodrigues" wrote:
>>
>> Is John Resig the author of JS Pro Techniques? If so, you might dismiss 
>> the book.
>>
> 
> Yes, it has John Resig's name on the front cover.
> 
> I take it that not many people here are fans of John Resig or his books
> or jQuery code or whatever?
> 

I'd say especially that book. It's very badly written, over and apart
from any technical mistakes and its basic lack of depth.

It was the 1st. book I read after 5+ years of Windows programming and I
think that although the topics were well chosen, the quality was very
low...

-- 

-williamc

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web