Skip to content

Instantly share code, notes, and snippets.

@merolhack
Forked from mathewbyrne/slugify.js
Last active May 29, 2023 11:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save merolhack/3b242fac97e4167ec2be to your computer and use it in GitHub Desktop.
Save merolhack/3b242fac97e4167ec2be to your computer and use it in GitHub Desktop.
Javascript Slugify: For accents and other latin characters
function slugify(text)
{
var from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;";
var to = "aaaaaeeeeeiiiiooooouuuunc------";
_.each( from, function( character, i ) {
text = text.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
});
return text
.toString() // Cast to string
.toLowerCase() // Convert the string to lowercase letters
.trim() // Remove whitespace from both sides of a string
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/&/g, '-y-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-'); // Replace multiple - with single -
}
@seballot
Copy link

seballot commented Jun 16, 2018

Great ! thank you :)
For those who don't use underscoreJs, replace _.each( from, function( character, i ) { by

for (var i = 0, len = from.length; i < len; i++)

@joaomantovani
Copy link

A more functional approach

function slugify(text) {
  const from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;"
  const to = "aaaaaeeeeeiiiiooooouuuunc------"

  const newText = text.split('').map(
    (letter, i) => letter.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)))

  return newText
    .toString()                     // Cast to string
    .toLowerCase()                  // Convert the string to lowercase letters
    .trim()                         // Remove whitespace from both sides of a string
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/&/g, '-y-')           // Replace & with 'and'
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-');        // Replace multiple - with single -
}

@radames
Copy link

radames commented Jul 10, 2020

This works for me

function slugify(text) {
    const from = 'ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;'
    const to = 'aaaaaeeeeeiiiiooooouuuunc------'

    const newText = Array.from(s)
      .map((c) => {
        const index = [...from].indexOf(c)
        if (index > -1) {
          return c.replace(
            new RegExp(from.charAt(index), 'g'),
            to.charAt(index)
          )
        }
        return c
      })
      .join('')


    return newText
      .toString()                     // Cast to string
      .toLowerCase()                  // Convert the string to lowercase letters
      .trim()                         // Remove whitespace from both sides of a string
      .replace(/\s+/g, '-')           // Replace spaces with -
      .replace(/&/g, '-y-')           // Replace & with 'and'
      .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
      .replace(/\-\-+/g, '-');        // Replace multiple - with single -

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