OpenAPI Path Template
Match OpenAPI / Express-style path templates with `{param}` (or `:param` after substitution) placeholders.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("\\/[\\w\\-]+(?:\\/(?:\\{[\\w\\-]+\\}|[\\w\\-]+))*\\/?", "g");
const input = "GET /users/{id}/posts/{postId}/comments";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"\/[\w\-]+(?:\/(?:\{[\w\-]+\}|[\w\-]+))*\/?")
input_text = "GET /users/{id}/posts/{postId}/comments"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\/[\w\-]+(?:\/(?:\{[\w\-]+\}|[\w\-]+))*\/?`)
input := `GET /users/{id}/posts/{postId}/comments`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
\/[\w\-]+(?:\/(?:\{[\w\-]+\}|[\w\-]+))*\/? (flags: g)Raw source: \/[\w\-]+(?:\/(?:\{[\w\-]+\}|[\w\-]+))*\/?
How it works
Examples
Input
GET /users/{id}/posts/{postId}/commentsMatches
/users/{id}/posts/{postId}/comments
Input
/api/v1/healthMatches
/api/v1/health
Input
no pathNo match
—Common use cases
- •OpenAPI spec validation
- •Express / Next.js dynamic-route extraction
- •Path-template comparison (match similar routes)
- •Auto-generating client SDKs from path templates
Related patterns
URL Path Segment
WebMatch individual `/segment` parts of a URL path, capturing each one.
Cookie Header Value
WebParse name=value pairs from an HTTP `Cookie:` header value.
CSS Class from `class=""` Attribute
WebExtract the value of a `class="..."` attribute from raw HTML, handling double or single quotes.
CSS Custom Property (Variable)
WebMatch CSS custom properties (variables) like `--brand-color` or `--font-size-lg`.