Created
April 12, 2019 08:48
-
-
Save dexterlabora/689f660cb06622c0028ff40507d7a025 to your computer and use it in GitHub Desktop.
Regex to replace URL path params with values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote this to parse Swagger / OpenAPI spec