JavaScript / ECMAScript

OpenAPI Path Template in JS

Match OpenAPI / Express-style path templates with `{param}` (or `:param` after substitution) placeholders.

Try it in the JS tester →

Pattern

regexJS
\/[\w\-]+(?:\/(?:\{[\w\-]+\}|[\w\-]+))*\/?   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
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).

How the pattern works

\/[\w\-]+ matches the first path segment. The repeating group (?:\/(?:\{[\w\-]+\}|[\w\-]+))* matches subsequent segments — either literal text or a `{param}` placeholder. \/? allows an optional trailing slash. Useful for path-to-handler routing tables.

Examples

Input

GET /users/{id}/posts/{postId}/comments

Matches

  • /users/{id}/posts/{postId}/comments

Input

/api/v1/health

Matches

  • /api/v1/health

Input

no path

No match

Same pattern, other engines

← Back to OpenAPI Path Template overview (all engines)