[QUOTE="Stevo said:
Stevo wrote on 22 apr 2008 in comp.lang.javascript:
function convert(d) {
return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
};
or
function convert(d) {
return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
};
both tested IE7.
I'd stick with what Gregor created. It's simple, efficient and easy to
understand by whoever gets to maintain the code in the future.
As you wish, Stevo. Why is it efficient?
I just mean that it's simple, efficient ENOUGH, and immediately obvious
(without more than 2 seconds of thought) what it's doing. If you know
what the split method does, then you know what the function is doing.
The above is simple javascript.
I wouldn't recomment having my code maintained
by someone that does not understand the above.
We see almost every day people coming here that are given the task by
their employers of changing some code that they don't understand. We
might think that's a dumb idea, but it's happening all the time.
I'm not a fan of regexp constructs because of their lack of readability
to those unfamiliar with them, and that is a large proportion of
programmers that just never get into them. I don't mind admitting that I
fall into that group too. So the only reason I know what your second
function is doing, is because I know what job it has to do from the OP's
original requirements. If I didn't have that knowledge, then I'd be
wondering what this $2$1 business is all about. I've deduced that what
you've got in parentheses is looking for digit, digit, forward-slash and
when it finds one it generates the $1 token, then it looks again for
digit, digit forward slash and generates the $2 token, then the replace
happens and $2 and $1 get swapped in the returned string. If I didn't
have those OP requirements, I can guarantee I'd be at wikipedia's
regular expression page trying to find out what's going on. Don't get me
wrong, I'm impressed with it, but I'd never use it.[/QUOTE]
I agree 100% with this. I almost never use regexp for just this reason,
because they are unreadable. This doesn't stop me writing large apps
which are efficient enough.