Maybe I’m not understanding something, but can somebody explain to me why the following code is behaving the way it is:
/^[1-9]\d*(\.\d{2})?$/.test('0.50') === false;
/^\.\d+$/.test('0.50') === false;
/^([1-9]\d*(\.\d{2})?)|(\.\d+)$/.test('0.50') === true; // <-- WTF?
What’s going on here? As you can see the 3rd expression is composed of the first two joined by the | operator. Since each one tests false, isn’t false | false == false ? Or am I missing a basic concept with regular expressions?
I’ve tried this in the browser, and with nodejs. Same result either way.
This is because of precedence of operators.
Your expression is
^[1-9][0-9]*(\.\d{2})?or\.\d+$.If you do the following:
you will get
falseas you expect.(Note extra $ and ^ at the boundaries).
More details: in your second part of last expression you no longer require
.to be the first character.