Home > Back-end >  What's wrong with this non digits only slug regex
What's wrong with this non digits only slug regex

Time:09-22

I'm trying to get a pattern match for slugs, these would be alphanumeric sequences hyphen separated.

They can be:

  • Number starting or ended, but not only digits
  • Can also be only alphabetic
  • Can also can be only one word

I got /[a-z0-9] (?:-[a-z0-9] )*$/ which almost works but it'll match. 3 or 10

For example:

  • 2020-best-year-of-world should match
  • best-of-world should match
  • best-of-2021 should match
  • 2021 should not match

I leave here a regex101 link with my test

CodePudding user response:

Apart from adding an anchor ^ to assert the start of the steing, you can add a negative lookahead to not only match digits, or else your pattern will also match only 2020.

^(?![\d] $)[a-z0-9] (?:-[a-z0-9] )*$

In parts, the pattern matches:

  • ^ Start of string
  • (?![\d] $) Negative lookahead, assert not only hyphens and digits to the right
  • [a-z0-9] Match 1 occurrences of a-z or 0-9
  • (?:-[a-z0-9] )* Optionally repeat the previous pattern preceded by -
  • $ End of string

Regex demo

CodePudding user response:

How about this:

/[a-z0-9] (-*[a-z]) (?:-[a-z0-9] )*$
  • It starts with a char or a number
  • It needs to contain char or - in the middle (the part I've added from your regex, in order to filter only digit)
  • Ends with char, digit

I wasn't sure if, for instance 2020-2021 should match. If it shouldn't, you should do an small edit on the middle part...

I've tested in the same tool you suggested

  • Related