Skip to content

Instantly share code, notes, and snippets.

@stamat
Created August 16, 2019 23:08
Show Gist options
  • Save stamat/830ab252b506b48797a230bd2f90eaf1 to your computer and use it in GitHub Desktop.
Save stamat/830ab252b506b48797a230bd2f90eaf1 to your computer and use it in GitHub Desktop.
Wordpress like simple sanitize_title for javascript. Not checking for accents, since it is a slow process.
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
function slug(s) {
s = s.toLowerCase().trim();
s = s.replace(/[^a-z0-9\s_-]/ig, '');
return s.replace(/[\s\uFEFF\xA0]+/ig, '-');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment