refactor and extract the RegExp escaping