Skip to content

Instantly share code, notes, and snippets.

@singhmohancs
Created May 14, 2017 16:02
Show Gist options
  • Save singhmohancs/87382ce718d7b0b0d71011177ca0251a to your computer and use it in GitHub Desktop.
Save singhmohancs/87382ce718d7b0b0d71011177ca0251a to your computer and use it in GitHub Desktop.
AngularJS - Slugify Filter
(function () {
'use strict';
angular
.module('app')
.filter('slugify', Filter);
function Filter() {
return function (input) {
if (!input)
return;
// make lower case and trim
var slug = input.toLowerCase().trim();
// replace invalid chars with spaces
slug = slug.replace(/[^a-z0-9\s-]/g, ' ');
// replace multiple spaces or hyphens with a single hyphen
slug = slug.replace(/[\s-]+/g, '-');
return slug;
};
}
})();
<a href="{{post._id}}/{{post.title | slugify}}">{{post.title}}</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment