Lasse Reichstein Nielsen wrote on 27 apr 2008 in comp.lang.javascript:
Evertjan. said:
var
re=/^(((500)|([1-4]\d\d)|([1-9]\d?))(px)?)$|^(((8[0-5])|([1-7]\d)|[1-
9])\%)$/;
Good example of why not to use RegExp's for numerical comparison
It's almost unreadable, and it still doesn't match 0px (although that
could be by design).
Tast was my design, it would be simpler without restricting the zero.
re=/^(((500)|([1-4]\d\d)|([1-9]?\d))(px)?)$|^(((8[0-5])|([1-7]?\d))\%)$/;
function testMe(f) {
var match = f.match(/^(\d+)(px|%)?$/);
if (!match) { return false; }
var num = +match[1];
switch(match[2]) {
case '%': return num <= 85;
case 'px':
default:
return num <= 500;
}
}
It seems that the present mood is, that because Regex is unreadable
for a later moronic reviewer of your code, so should not be used.
Not hard to read, but hard to modify too, although that is related.
I do. Making code maintainable is important, to later "moronic
reviewers" (aka, your esteemed collegues and yourself in 30 days).
No, it is only important in your environment, but not in mine.
I, and many others I surmize, code for personal joy, compactness and
elegance, and mastering the Regex is a joy in itself.
I could not care less, if my code is unreadable if I am not around.
Throw it away, throw it away, throw it away!
After a few years I love to start from scratch and incorporate all the
new methods I learned or developed to get the same result perhaps in a
more elegant way.
Try changing the limit from 85 to 125, and consider how many places
you need to change. Maintainable code should only need to change in
one place, where "85" is replaced by "125".
Ah, an added challenge, I would like that.
OK, warn for lack of maintainability,
but don't dismiss the pleasure of such coding per se.
In my assembler days, each line had one or more comment lines to make it
readable, but those days are gone. Dreadfull sorry, Clementine? Perhaps.
However I am not even against the Javascript equivalents of the ancient
BASIC left(), mid(), right() and the less old instr(). With a lot of
code, you can get the same results.
3 or 4 years ago I could not "do" regex, and I didn't miss it, but you
never miss what you do not know.