The following line is in the jQuery source code:
// Used for matching numbers
core_pnum = /[\-+]?(?:\d*\.|)\d+(?:[eE][\-+]?\d+|)/.source,
I am confused by the last vertical bar in the two non-capturing groups,
(?:\d*\.|)
and
(?:[eE][\-+]?\d+|)
The regular expression /(a|b)/ matches a or b, so I wondered what /(a|b|)/ matches, and it seems to “match everything”, in other words
reg1 = /(a|b)/;
reg1.test('c'); // false
reg2 = /(a|b|)/;
reg2.test('c'); // true
What is going on?
I’ll try to break this down into chunks:
[\-+]?: This matches a plus sign, a minus sign or neither.(?:\d*\.|): This groups (the?:makes it a non-capturing group) any number of digits followed by a dot or nothing at all.\d+: This matches one or more consecutive digits.(?:[eE][\-+]?\d+|): This groups a lowercase or uppercase “e”, possibly followed by a plus or a minus and all of that followed by digits. Or, nothing.(a|)looks forafirst. Ifadoesn’t exist, it matches nothing. It’s a confusing way of writing(a)?.This regex is a little confusing. I’d re-write it like this: