Data and text · Easy · 10 min

Clean slugs for Spanish titles

Accents, ñ and opening punctuation end up as broken URLs. Turn them into a readable slug.

Write slugify(title). Strip accents (á becomes a, ñ becomes n, ü becomes u), lowercase everything, replace any run of characters that are not letters a-z or digits with a single hyphen, and trim hyphens from both ends.

Example: '¿Qué es la IA local?' gives 'que-es-la-ia-local'.

Challenges 0/4

  • '¿Qué es la IA local?' gives 'que-es-la-ia-local'
  • Converts ñ and accents, keeps numbers
  • Collapses repeated separators and trims the ends
  • Removes the diaeresis and symbols

function slugify(title) {
  // 1. normalize('NFD') and drop the combining marks
  // 2. lowercase, 3. non-alphanumerics to '-', 4. trim hyphens
  return title.toLowerCase().replace(/ /g, '-');
}

console.log(slugify('¿Qué es la IA local?'));
Console output appears here (console.log).

Go deeper: the MDN reference →

This in production, with your data? Let's talk for 15 minutes →