function escape Regex Characters(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } code example
Example: javascript escape regex
function escapeRegExp(input) {
const source = typeof input === 'string' || input instanceof String ? input : '';
return source.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}