スラグ生成器
任意のテキストをクリーンなURL対応スラグに変換
入力テキスト
スラグ
URLスラグとは?
URLスラグとは、ウェブアドレスの中で特定のページを人間が読める形で識別する部分です。URLが https://example.com/blog/my-first-post の場合、スラグは my-first-post にあたります。スラグ生成器はページのタイトルや説明文を、小文字・数字・ハイフン(または別のセパレーター)のみで構成される文字列に変換します。このプロセスではスペースの除去、アクセント記号や発音区別符号の削除、特殊文字の除去、連続する空白の単一セパレーターへの正規化が行われます。
スラグ生成はコンテンツ管理システム、静的サイトジェネレーター、ブログプラットフォーム、そしてユーザー入力からURLを導出するあらゆるアプリケーションにおける標準的な処理です。WordPress、Ghost、Hugo、Next.js、Djangoはすべてスラグ生成のロジックを内蔵しています。読みやすいURLはユーザビリティと検索エンジン最適化の両方を向上させるためです。適切に形成されたスラグは、ユーザーがリンクをクリックする前にページの内容を伝えます。
「スラグ」という用語は新聞出版業界に由来し、制作中の記事を識別するための短いラベルとして使われていました。ウェブ開発においても、スラグは同じ機能を果たします。長いタイトルや名前から導出される、コンパクトで一意のURL安全な識別子です。スラグはプログラムで導出されるため、信頼性の高いスラグ生成器はすべてのページとロケールで一貫性を保証します。
このスラグ生成器を使う理由
URLスラグを手作業で作成するとミスが起きやすいです。アクセント記号の削除忘れ、二重ハイフンの残存、Unicode入力のエッジケース見落としなどが、壊れた見栄えの悪いURLにつながります。このツールはこれらの問題を自動的に処理します。
スラグ生成器の活用例
スラグ生成のルールと文字処理
スラグ生成は予測可能な一連の変換ステップに従います。各ステップを理解することで、予期しない出力のデバッグや独自のslugify関数の実装に役立ちます。
- 1. Unicode正規化(NFD)合成文字を基底文字と結合マークに分解します。たとえば é(U+00E9)は e と結合アキュートアクセント(U+0301)になります。これにより次のステップで発音区別符号を除去できるようになります。
- 2. 発音区別符号の除去Unicode結合発音区別符号ブロック(U+0300〜U+036F)のすべての文字を除去します。このステップ後、caféはcafeに、ÑはNになります。
- 3. 特殊文字の除去文字・数字・空白・ハイフン以外のすべての文字をスペースに置換します。これにより句読点・記号・ASCII相当のない文字が除去されます。
- 4. 先頭・末尾の空白削除と空白の統合先頭と末尾の空白を削除し、連続するスペース・アンダースコア・ハイフンの列を1つの選択したセパレーターに統合します。
- 5. 大文字小文字とセパレーターの適用小文字に変換(オプション)し、選択したセパレーター文字(ハイフン -、アンダースコア _、またはドット .)で単語を結合します。
文字変換リファレンス
下の表はスラグ生成時に一般的な文字がどのように処理されるかを示しています:
| 入力 | 出力 | 適用ルール |
|---|---|---|
| Hello World | hello-world | Lowercase + space → hyphen |
| Café au Lait | cafe-au-lait | NFD normalization strips é → e |
| naïve résumé | naive-resume | Multiple diacritics removed |
| Price: $9.99! | price-9-99 | Symbols ($, !, :) removed |
| too many | too-many | Whitespace trimmed and collapsed |
| one--two___three | one-two-three | Mixed separators collapsed |
| Привет мир | privet-mir | Cyrillic (if transliteration) or removed |
| file_name.txt | file-name-txt | Dots and underscores replaced |
| React & Vue | react-vue | Ampersand removed |
| 2026-03-30 | 2026-03-30 | Digits and hyphens preserved |
コード例
主要な言語とフレームワークでのスラグ生成の実装例です。各例ではUnicode正規化・発音区別符号の除去・セパレーターの挿入を処理しています。
function slugify(text, separator = '-') {
return text
.normalize('NFD') // decompose accented chars
.replace(/[\u0300-\u036f]/g, '') // strip diacritics
.toLowerCase()
.replace(/[^\w\s-]/g, ' ') // drop special chars
.trim()
.replace(/[\s_-]+/g, separator) // collapse whitespace → separator
// slugify('Café au Lait') → "cafe-au-lait"
// slugify('Hello World', '_') → "hello_world"
}
// Node.js alternative using the `slugify` npm package:
// npm install slugify
// const slugify = require('slugify')
// slugify('Hello World', { lower: true, strict: true }) → "hello-world"import re
import unicodedata
def slugify(text: str, separator: str = '-') -> str:
"""Convert text to a URL-safe slug."""
text = unicodedata.normalize('NFD', text)
text = text.encode('ascii', 'ignore').decode('ascii') # strip non-ASCII
text = text.lower()
text = re.sub(r'[^\w\s-]', ' ', text)
text = text.strip()
text = re.sub(r'[\s_-]+', separator, text)
return text
# slugify('Café au Lait') → "cafe-au-lait"
# slugify('Hello World', '_') → "hello_world"
# Alternative: python-slugify (pip install python-slugify)
# from slugify import slugify
# slugify('Café au Lait') → "cafe-au-lait"package main
import (
"regexp"
"strings"
"unicode"
"golang.org/x/text/unicode/norm"
"golang.org/x/text/transform"
"golang.org/x/text/runes"
)
func slugify(text string) string {
// NFD normalize and strip diacritics
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ := transform.String(t, text)
result = strings.ToLower(result)
re := regexp.MustCompile(`[^\w\s-]+`)
result = re.ReplaceAllString(result, " ")
result = strings.TrimSpace(result)
re = regexp.MustCompile(`[\s_-]+`)
result = re.ReplaceAllString(result, "-")
return result
}
// slugify("Café au Lait") → "cafe-au-lait"
// slugify("Hello World") → "hello-world"function slugify(string $text, string $separator = '-'): string {
// Transliterate non-ASCII characters
$text = transliterator_transliterate(
'Any-Latin; Latin-ASCII; Lower()', $text
);
// Remove anything that is not a word char, space, or hyphen
$text = preg_replace('/[^\w\s-]/', ' ', $text);
$text = trim($text);
$text = preg_replace('/[\s_-]+/', $separator, $text);
return $text;
}
// slugify('Café au Lait') → "cafe-au-lait"
// slugify('Hello World', '_') → "hello_world"