I cannot figure out this javascript little oddity that’s occurs in Firefox 7.0.1 and Google Chrome 14.0.835.202 (I haven’t tested any other versions). Why does /[+-.]/g match commas (,) in addition to plus signs (+), dashes (-) and periods (.)?
// Firebug
>>> "Hello, World++--..".match(/[+-.]/g);
[",", "+", "+", "-", "-", ".", "."]
>>> "Hello, World".match(/[+-.]/g);
[","]
// Chrome Developer Tools:
> "Hello, World++--..".match(/[+-.]/g);
[",", "+", "+", "-", "-", ".", "."]
> "Hello, World".match(/[+-.]/g);
[","]
Okay, so maybe I need to escape the period (.)
// Firebug
>>> "Hello, World!".match(/[+-\.]/g);
[","]
// Chrome Developer Tools
> "Hello, World!".match(/[+-\.]/g);
[","]
Nope. But if I change the order of the plus (+) and dash (-) it stops matching the comma (,).
// Firebug
>>> "Hello, World".match(/[-+.]/g);
null
// Chrome Developer Tools
> "Hello, World".match(/[-+.]/g);
null
This makes no sense to me. It seems odd that both Firefox and Chrome would share the same regex bug. Does anybody know why this is?
Use
[+\-.].-masks a range and must be escaped.