Skip to content

Instantly share code, notes, and snippets.

@zachstronaut
Created January 30, 2012 18:24
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zachstronaut/1705796 to your computer and use it in GitHub Desktop.
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