JavaScript RegExp Oddities
While fooling around with some RegExp, I realized that those actually were functions. I thought it was quite funny and I started toying a little with them, and came across this very unusual result:
// applying a regexp and matching against it seem to be the same…
/(a+)/("aaaaa"); // == ["aaaaa", "aaaaa"];
"aaaaa".match(/(a+)/); // == ["aaaaa", "aaaaa"];
/(a+)/("aaaaa"); // == ["aaaaa", "aaaaa"];
"aaaaa".match(/(a+)/); // == ["aaaaa", "aaaaa"];
// except when using the "global" flag…
/(a+)/g("aaaaa"); // == ["aaaaa", "aaaaa"];
"aaaaa".match(/(a+)/g); // == "aaaaa"
I'm really trying hard to figure out why I get these results, but just can't.
Does anyone have an idea ?