Factor out ORDER BY direction detection/stripping

The slightly unobvious regex is so that it's easier to add support for
more clauses (e.g. NULLS FIRST/LAST) later.