Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 321
Royi Namir Avatar asked Dec 04 '13 16:12

Royi Namir


1 Answers

\\. 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.
like image 65
Kobi Avatar answered Nov 04 '22 21:11

Kobi