Search code examples
javascriptregexsizzle

Sizzle's Javascript regex for CLASS?


I've looked at Prototype.js code and I saw there ( at the Sizzle part) :

enter image description here

My question is about that line :

CLASS: /\.((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,

the \. is for dot , next is (uncaptured group with : words, range and -) OR (\.). ( actually it says \\. but the first one is just for escaping so it's \.).

Huh ?

What's \. ?

I did test /\.((?:[\w\u00c0-\uFFFF\-]|\\.)+)/.test('.aa\.') and it returns true.

But what is .aa\. ? obviously the class is .aa but if \ is a valid char , why it isn't at the [..] section ?

What am I missing ?


Solution

  • \\. matches a literal backslash, followed by any character (the dot is not escaped).

    From http://Sizzlejs.com/:

    Escaped selector support #id\:value

    It is used to match classes like a\~b, and it is actually repeated in most selectors on your screenshots. It is common usually when you have dots or brackets in names or classes.

    As for your test:

    • In JavaScript, "invalid" escape sequences are ignored. "\." === ".", and your test is the same as .test('.aa.').
    • .test allows partial matching - /\w+/.test("a!") === true - it doesn't mean the last dot was actually matched.