1function escapeRegExp(input) {
2 const source = typeof input === 'string' || input instanceof String ? input : '';
3 return source.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
4}
5
1// Tests website Regular Expression against document.location (current page url)
2if (/^https\:\/\/example\.com\/$/.exec(document.location)){
3 console.log("Look mam, I can regex!");
4}
1var format = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
2// ^ ^
3document.write(format.test("My@string-with(some%text)") + "<br/>");
4document.write(format.test("My string with spaces") + "<br/>");
5document.write(format.test("MyStringContainingNoSpecialChars"));