optimised js function capturing regex