Escape string for use in Javascript regex [duplicate]

Short ‘n Sweet (Updated 2021) To escape the RegExp itself: function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, ‘\\$&’); // $& means the whole matched string } To escape a replacement string: function escapeReplacement(string) { return string.replace(/\$/g, ‘$$$$’); } Example All escaped RegExp characters: escapeRegExp(“All of these should be escaped: \ ^ $ * + ? . ( … Read more

Escaping HTML strings with jQuery

There is also the solution from mustache.js var entityMap = { ‘&’: ‘&amp;’, ‘<‘: ‘&lt;’, ‘>’: ‘&gt;’, ‘”‘: ‘&quot;’, “‘”: ‘&#39;’, “https://stackoverflow.com/”: ‘&#x2F;’, ‘`’: ‘&#x60;’, ‘=’: ‘&#x3D;’ }; function escapeHtml (string) { return String(string).replace(/[&<>”‘`=\/]/g, function (s) { return entityMap[s]; }); }

Saving UTF-8 texts with json.dumps as UTF-8, not as a \u escape sequence

Use the ensure_ascii=False switch to json.dumps(), then encode the value to UTF-8 manually: >>> json_string = json.dumps(“ברי צקלה”, ensure_ascii=False).encode(‘utf8’) >>> json_string b'”\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94″‘ >>> print(json_string.decode()) “ברי צקלה” If you are writing to a file, just use json.dump() and leave it to the file object to encode: with open(‘filename’, ‘w’, encoding=’utf8′) as json_file: json.dump(“ברי צקלה”, json_file, … Read more

HTML-encoding lost when attribute read from input field

EDIT: This answer was posted a long ago, and the htmlDecode function introduced a XSS vulnerability. It has been modified changing the temporary element from a div to a textarea reducing the XSS chance. But nowadays, I would encourage you to use the DOMParser API as suggested in other anwswer. I use these functions: function … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)