On May 14, 8:24 pm, Gabriel Gilini <gabr...@usosim.com.br> wrote:
> On Wed, May 13, 2009 at 3:34 PM, louis w <louiswa...@gmail.com> wrote:
>
> > I have an array containing a number of strings. I would like to
> > continue exucuting my script only if an item has ANY/ALL of the
> > strings assigned as a class name.
>
> > $A(['foo', 'bar', 'foob']);
>
> > Is there an elegant want to do this without having to loop through the
> > items?
>
> Supposing you have the aforementioned element stored in the - elm -
> variable, and the classes' array in the - classesArr - variable:
>
> var classNamesRegex = new RegExp('\\b(' + classesArr.join('|') + ')\\b'); //
> Generates /\b(foo|bar|foob)\b/
>
> if(!classNamesRegex.test(elm.className)){
>    window.alert('get me out of here');
>
> }

Please don't use boundaries to separate class values. I wonder who
came up with this silly idea and why it keeps circulating around.

/\bfoo\b/ matches values such as "foo-bar" which is, of course, wrong.
Class tokens are separated by whitespace (well, technically there's a
particular set of characters. HTML5, for example, defines them as - [\
\u0020\\u0009\\u000A\\u000C\\u000D]) and so regex to match certain
className should be wrapped with whitespace characters (as well as
"start of line"/"end of line" tokens) -

/(^|\s)foo(\s|$)/

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to