Javascript refactored + regexp substitution