Do not try escape characters with replace/replaceAll because there is a lot corner cases. Instead, you should use encodeURIComponent (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent). It will replace characters such as &, \ correctly which is already globally tested: