I need a regular expression for a URL-friendly slug, which can only contain lowercase letters and hyphens. It can't start or end with a hyphen and can't have more than one consecutive dash.
It will be used in a data annotation in C#:
[RegularExpression("", ErrorMessage = "Slug can only contain lowercase letters and dash; must not start or end with a dash; must not have more then 1 consecutive dash.")] I tried the following expressions from this question.
/^[a-z0-9]+(?:-[a-z0-9]+)*$/ /^[a-z0-9]+(?:[_-][a-z0-9]+)*$/ - How can I modify the expressions to validate what I need?
- Can someone further explain how the non-capture group works in this case? I didn't understand the explanation I got from Google.
c-sharp (valid) c-sharp-code (valid) -csharp (invalid) csharp- (invalid) c--sharp (invalid) csharp9 (invalid) c_sharp (invalid)
+) too, but without the cost of it being a normal capturing group.^[a-z]+(?:-[a-z]+)*$regex101.com/r/a05kLh/1