Regular Expressions 101

Community Patterns

URI slug with optional slashes and query string

0

Regular Expression
PCRE (PHP <7.3)

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

Description

Validates a URI slug.

Must:

  • Contain lowercase alphanumeric character(s) separated with a single dash.

Optionally allows:

  • Alphanumeric words and dashes separated by a single forward slash
  • Any valid query string

Must NOT:

  • Contain single character between dashes
  • Contain only numbers and dashes.
  • Begin with a number or a dash.
  • End with a dash.

Forked from https://regex101.com/library/caXjPb

Submitted by Quentin McRee - 7 years ago