URL Slug Generator Rules: Lowercase, Hyphens, Clean Slugs

PublishedToolSlug Generator

Contents
  1. Why you shouldn't use spaces and special characters as-is
  2. The rules for converting to a slug: doing it by hand
  3. Common trouble spots
  4. Recap
  5. Frequently asked questions

If you put a post title or any random sentence straight into a web address, spaces turn into percent-encoded strings like %20, and once special characters get mixed in, the URL becomes long and messy. The standard way to clean this up is slug conversion.

This post lays out the rules for turning a string into a slug, complete with the actual regular expressions, and explains how the result changes depending on whether you keep Korean text or strip it out.

Why you shouldn't use spaces and special characters as-is

If unencoded spaces or special characters go straight into the browser address bar, the browser converts them into hexadecimal strings called percent encoding so it can communicate with the server.

  • Before conversion: my page 1
  • Browser auto-encoding: my%20page%201 (spaces become %20, so the address gets messy)
  • Slug conversion: my-page-1 (joined with hyphens, short, and stays intact when copied)

The rules for converting to a slug: doing it by hand

The procedure for turning a string into a slug goes in this order.

  1. Make everything lowercase. Convert every uppercase letter to lowercase.
  2. Remove characters that aren't allowed. Keep only lowercase letters, digits, spaces, and hyphens, and delete the remaining special characters (commas, periods, at signs, and so on).
  3. Turn spaces and underscores into hyphens, then collapse repeated hyphens into one. After that, clean up any hyphens left at the start or end of the string.

Put into code, it becomes the following chain of regular expressions.

function slugify(text) {
  return text
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9\s-]/g, "")   // remove characters not on the allowed list
    .replace(/[\s_]+/g, "-")        // spaces and underscores → hyphens
    .replace(/-+/g, "-")            // collapse repeated hyphens
    .replace(/^-+|-+$/g, "");       // trim hyphens from both ends
}

Sentences that contain Korean work a little differently. If you add Korean syllables (가-힣) to the allowed-character list in step 2, Korean stays in place instead of being deleted. Conversely, if you want to strip Korean too and keep only a letter-and-number slug, just remove Korean from that list.

Instead of assembling the regex by hand every time, paste your sentence into the tool below and compare both results side by side.

Slug Generator

Type a title or sentence you want to use in a URL, choose whether to keep Korean characters, and it instantly builds a lowercase hyphen slug.

This tool switches between two modes with the "Keep Korean" checkbox. When it's on, Korean is left alone and only spaces and special characters are cleaned up; when it's off, Korean is removed as well, leaving only letters, digits, and hyphens. The default is to keep Korean.

Common trouble spots

SituationHow it's handled
Multiple spaces in a rowCollapsed into a single hyphen
Underscores (_) includedConverted to hyphens
Hyphens left at the start or endRemoved automatically
Two or more posts with the same titleSlugs collide, so add a number or date at the end to tell them apart

The custom of using hyphens instead of underscores comes from reports that some search engine crawlers read an underscore-joined string as one word and a hyphen-joined string as multiple words. Keep in mind, though, that this is a convention for URL readability, not a rule that guarantees search ranking.

Recap

  • A slug cleans up spaces and special characters into a mix of hyphens and lowercase letters, making the URL short and easy to read.
  • If you remember just the four regex steps (lowercase → remove special characters → collapse hyphens → trim both ends), you can implement it yourself.
  • Whether to keep Korean depends on the nature of your service, so it's safer to preview the result and then choose.
Slug Generator

Type a title or sentence you want to use in a URL, choose whether to keep Korean characters, and it instantly builds a lowercase hyphen slug.

Frequently asked questions

Can I make a slug from a Korean title?

Yes. Turn on the "Keep Korean" option and Korean stays as-is while spaces and disallowed special characters are cleaned up. If you need a slug made only of letters and digits, turn the option off to remove Korean as well.

What if a hyphen is left at the end of the slug?

Use the last step of the regex (.replace(/^-+|-+$/g, "")) to strip the hyphens left at the start and end of the string. Slugs the tool produces already include this cleanup.

What happens if the same slug collides with another one?

The routing system can't tell which post to open. In practice, you avoid the conflict by appending a number or a date string to the end of the slug.

This article was adapted from our Korean original. Read the Korean original

Related articles

  1. Smart Quotes Cleaner: Convert Curly Quotes to ASCIIWord and messenger editors turn straight quotes into curly ones, which breaks code. Here's why it happens and how to convert them back to plain ASCII.
  2. Browser Mic Dictation: Why It Works in Chrome but Not FirefoxUse the Web Speech API to dictate mic audio into text in your browser with no install, see which browsers support it, and learn why Firefox can't.
  3. Split Text Into Sentences: Punctuation Rules and Line BreaksHow sentence splitting works: split on . ? ! and …, skip decimals and common abbreviations like Dr. and e.g., and format the output as lines or lists.
  4. SQL DDL to JSON Schema: Type Mapping and Parsing RulesHow SQL types map to JSON Schema Draft-07, how NOT NULL and PRIMARY KEY become required, and which constraints you must add by hand.
  5. SQL Formatter Rules: How Your One-Line Query Gets SortedSee exactly how a SQL formatter rewrites your query: which keywords get uppercased, where lines break, what gets indented, and why the results stay the same.
  6. SQL Parameter Binding: How Values Fill PlaceholdersSee how ?, :id, and @name placeholders get real values: numbers unquoted, strings quoted and escaped, and ? inside comments or strings left alone.

Related tools