DRY out code and use shared function; fix regex for edge case per feedback on WEB-8724