Skip to content

Instantly share code, notes, and snippets.

@gabrielfroes
Forked from mathewbyrne/slugify.js
Last active February 6, 2024 23:36
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save gabrielfroes/e90a53f96ed71fb201d133395003ada4 to your computer and use it in GitHub Desktop.
Save gabrielfroes/e90a53f96ed71fb201d133395003ada4 to your computer and use it in GitHub Desktop.
Javascript Slugify
/*
Create SLUG from a string
This function rewrite the string prototype and also
replace latin and other special characters.
Forked by Gabriel Froes - https://gist.github.com/gabrielfroes
Original Author: Mathew Byrne - https://gist.github.com/mathewbyrne/1280286
*/
if (!String.prototype.slugify) {
String.prototype.slugify = function () {
return this.toString().toLowerCase()
.replace(/[àÀáÁâÂãäÄÅåª]+/g, 'a') // Special Characters #1
.replace(/[èÈéÉêÊëË]+/g, 'e') // Special Characters #2
.replace(/[ìÌíÍîÎïÏ]+/g, 'i') // Special Characters #3
.replace(/[òÒóÓôÔõÕöÖº]+/g, 'o') // Special Characters #4
.replace(/[ùÙúÚûÛüÜ]+/g, 'u') // Special Characters #5
.replace(/[ýÝÿŸ]+/g, 'y') // Special Characters #6
.replace(/[ñÑ]+/g, 'n') // Special Characters #7
.replace(/[çÇ]+/g, 'c') // Special Characters #8
.replace(/[ß]+/g, 'ss') // Special Characters #9
.replace(/[Ææ]+/g, 'ae') // Special Characters #10
.replace(/[Øøœ]+/g, 'oe') // Special Characters #11
.replace(/[%]+/g, 'pct') // Special Characters #12
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
};
}
@wellingtonlinslima
Copy link

muito legal

@wellingtonlinslima
Copy link

gostei bastante

@neto-developer
Copy link

Show cara, valeu por compartilhar. Muito útil.

@GenesesLopes
Copy link

Salvou minha vida! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment