Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An HTMLSAFE and SLUGIFY in JavaScript
/**
* SLUGIFY - Attempts to match Django slugify filter: "Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace."
*/
function SLUGIFY(str) {
return str.toString().toLowerCase().replace(/^\s+|\s+$/g,'').replace(/\s/g, '-').replace(/[^-_a-z0-9]/g, '');
}
/**
* HTMLSAFE - Convert &<>" to HTML entities
*/
function HTMLSAFE(str) {
return str.toString().replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment