Reputation: 2185
I'm trying to replace a value in a string with a double curly (used by postman for variable substitution), but every time I try to quote or escape the braces, I always get additional escaped quotes or double escaped braces, all of which break the substitution:
Original String:
"header": [{"key": "x-device-auth","value": "\"token\""}]
OriginalString.replace('token','{{token}}')
Result:
"header":[{"key":"x-device-auth","value":"\"{{token}}\""}]
If I search for .replace('\"token\"','{{token}}')
, I don't get a match. The final string needs to be:
"header": [{"key": "x-device-auth","value": "{{token}}"}]
Upvotes: 1
Views: 1510
Reputation: 1809
You should be looking for token
with the escaped wrapping double quotes, since you also want to replace those.
var originalString = '"header": [{"key": "x-device-auth","value": "\\"token\\""}]';
console.log(originalString);
console.log(originalString.replace('\\"token\\"','{{token}}'));
originalString = '"header": [{"key": "x-device-auth","value": "\"token\""}]';
console.log(originalString);
console.log(originalString.replace('"token"','{{token}}'));
I have added two cases, one with the original string actually containing backslashes (first originalstring definition). The second without. Choose the one, that best matches your actual input :-)
Upvotes: 2
Reputation: 368
I dont see the input string is having proper escape characters applied. As you posted question in javascript tag, I tried below with javascript and its giving required results.
var str = "\"header\": [[{\"key\": \"x-device-auth\",\"value\": \"token\"}]";
var res = str.replace('token','{{token}}');
Upvotes: 0