{"id":15,"date":"2016-01-09T06:26:31","date_gmt":"2016-01-09T06:26:31","guid":{"rendered":"http:\/\/muthu.co\/?p=15"},"modified":"2021-01-02T14:06:16","modified_gmt":"2021-01-02T14:06:16","slug":"slugify-urls-using-javascript","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/slugify-urls-using-javascript\/","title":{"rendered":"Slugify URLs using Javascript"},"content":{"rendered":"

To generate human-readable url slugs from any ordinary string, you can uyse the following code snippet in your javascript. You can use it as a function or as an angular factory method<\/p>\n

Javascript Function:<\/p>\n

var slugify = function(text){\r\n        return text.toString().toLowerCase()\r\n               .replace(\/\\s+\/g, '-')           \/\/ Replace spaces with -\r\n               .replace(\/[^\\w\\-]+\/g, '')       \/\/ Remove all non-word chars\r\n               .replace(\/\\-\\-+\/g, '-')         \/\/ Replace multiple - with single -\r\n               .replace(\/^-+\/, '')             \/\/ Trim - from start of text\r\n               .replace(\/-+$\/, '');            \/\/ Trim - from end of text\r\n        }<\/pre>\n

Angular Method:<\/p>\n

angularAppName.factory('AppUtil', function(){\r\n     return {\r\n         slugify: function(text){\r\n              return text.toString().toLowerCase()\r\n                  .replace(\/\\s+\/g, '-')           \/\/ Replace spaces with -\r\n                  .replace(\/[^\\w\\-]+\/g, '')       \/\/ Remove all non-word chars\r\n                  .replace(\/\\-\\-+\/g, '-')         \/\/ Replace multiple - with single -\r\n                  .replace(\/^-+\/, '')             \/\/ Trim - from start of text\r\n                  .replace(\/-+$\/, '');            \/\/ Trim - from end of text\r\n              }\r\n         }               \r\n });<\/pre>\n

  <\/p>\n","protected":false},"excerpt":{"rendered":"

To generate human-readable url slugs from any ordinary string, you can uyse the following code snippet in your javascript. You can use it as a function or as an angular factory method Javascript Function: var slugify = function(text){ return text.toString().toLowerCase() .replace(\/\\s+\/g, ‘-‘) \/\/ Replace spaces with – .replace(\/[^\\w\\-]+\/g, ”) \/\/ Remove all non-word chars .replace(\/\\-\\-+\/g, […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[52,3,4,5],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/15"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":1,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":1682,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/15\/revisions\/1682"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}