Skip to content

Instantly share code, notes, and snippets.

@quisido
Created August 3, 2018 17:39
Show Gist options
  • Select an option

  • Save quisido/88ce106b568d638486b9a7783749da0f to your computer and use it in GitHub Desktop.

Select an option

Save quisido/88ce106b568d638486b9a7783749da0f to your computer and use it in GitHub Desktop.
PHP's htmlspecialchars implemented in JavaScript
/********************************\
|* Translating ampersand first: *|
\********************************/
var string = '<'; // <
string = string.replace(/&/g, '&amp;'); // <
string = string.replace(/</g, "&lt;"); // &lt;
/*******************************\
|* Translating ampersand last: *|
\*******************************/
var string = "<"; // <
string = string.replace(/</g, "&lt;"); // &lt;
string = string.replace(/&/g, "&amp;"); // &amp;lt;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment