Best way to match vanity URLs with an "s" or no "s" at the end

72 Views Asked by At

A common problem when creating a vanity or redirect URL is that users might leave the plural off the end.

What's the best way to match all of the following?:

  • /parksprojects
  • /parksproject
  • /parkproject
  • /parkprojects

^/park[s|*]project*$ ?

Any suggestions are appreciated, thanks in advance!

1

There are 1 best solutions below

3
Nicola Mustone On

You can use ^\/parks?projects?$. This regex matches all the cases in your example, tested on regex101.com

The ? after the s means that the letter is optional and might not be there.

If you want the regex to match your string anywhere in the content, such as an URL or as part of a more complex string, you can use \/parks?projects? without beginning and ending markers.

regex101.com proof that the regex works

If you have more examples I could be more precise.