URL Slug Regex

Matches URL-friendly slugs using lowercase letters, numbers, and hyphens.

パターン

/^[a-z0-9]+(?:-[a-z0-9]+)*$/
→ ビジュアライザーで開く

テスト例

my-blog-post hello-world-123 Invalid Slug another-valid-slug

コード例

javascript

const regex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
const isValid = regex.test(str);

python

import re
pattern = re.compile(r'^[a-z0-9]+(?:-[a-z0-9]+)*$')
is_valid = bool(pattern.match(text))

go

import "regexp"
re := regexp.MustCompile(`^[a-z0-9]+(?:-[a-z0-9]+)*$`)
isValid := re.MatchString(text)
urlslugvalidation