views:

45

answers:

1

What is this regex supposed to do, because it keeps giving back null?

var klass = this.rel.match(/facebox\[?\.(\w+)\]?/)

I thought it was extracting extra words from the rel attribute?

thanks, Richard

+3  A: 

Matches

facebox   // facebox
\[?       // [ or nothing
\.        // .
(\w+)     // word*
\]?       // ] or nothing

Valid inputs:

facebox[.bb
facebox.bb]
facebox[.bb]
facebox.bb

Invalid inputs

facebox[bb]
faceboX[.bb]
Facebox.bb]

About \w *

Matches any word character. Equivalent to the Unicode character categories [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9].

Reference

BrunoLM
How would you explain your evauation of the situation to somebody new to Regex: or in layman's terms? I think such an addition to your orgininal answer would make it invaluable to future visitors, and garner more upvotes.
John K
that is a perfect response except I think you meant to say that (\w+) matches a single word, and not "words"(unless they are separated by underscores) I believe it is the equivalent of ([a-zA-Z0-9_]+).
gnomed
@gnomed: Thanks :). I've updated my answer extending the explanation for `\w`. Thanks for the hint.
BrunoLM
That paragraph you quoted is talking about the .NET regex flavor. The OP is using JavaScript, so ECMAScript mode is the *only* mode.
Alan Moore
Now the regex is working. The browsers cache needed to be empty'd.
Richard