Skip to content

Instantly share code, notes, and snippets.

@dexterlabora
Created April 12, 2019 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexterlabora/689f660cb06622c0028ff40507d7a025 to your computer and use it in GitHub Desktop.
Save dexterlabora/689f660cb06622c0028ff40507d7a025 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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