Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
^(?![0-9-]+$)(?:[a-z]{2,}-?|[0-9]-?)+(?<!-)$
/
gm

Description

This slug regex is optimized using strictly my own opinions.

Slugs:

Should be english, alphanumeric phrases separated with a single dash. Should not contain single alpha character between dashes. Should not be comprised of just numbers and dashes. Should not begin with a number or a dash. Should not end with a dash.

2+ alpha character lookup increases expression speed greatly over allowing 1+ alpha lookups, 3+ improves it more although marginally.)

Submitted by Jordon Baade - 8 years ago