Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Regex to replace URL path params with values
var path = '/network/{id}/device/{serial}';
params = {
id:"123456",
serial: "abcd-abcd-abcd"
}
var paramNames = Object.keys(params);
console.log('paramNames', paramNames);
var mapping = {};
paramNames.forEach((e,i) => mapping[`{${e}}`] = params[e]);
newPath = path.replace(/\{\w+\}/ig, n => mapping[n]);
console.log("path", path);
console.log('netPath', newPath);
@dexterlabora

This comment has been minimized.

Copy link
Owner Author

@dexterlabora dexterlabora commented Apr 12, 2019

I wrote this to parse Swagger / OpenAPI spec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment