Objeto de muestra (el siguiente objeto es un objeto anidado muy grande):
const obj = { a: 1, url: "https://someurl.com", b: { c: 1, d: { otherUrl: "https://someotherurl.com"}}; Quiero agregar algún parámetro de consulta al final de cada URL, como ?d=1234 .
Intenté hacerlo, pero las URL están cambiadas:
const convertedObj = JSON.stringify(obj); let bufferObj; const fullRegex = /url":"([^"]*)/g; const fullMatch = convertedObj.match(fullRegex); if(fullMatch) { fullMatch.forEach((str, i) => { bufferObj = convertedObj.replace(/(url|Url)":"([^"]*)/g, `${str}?d=1234`); }); }¿Hay una solución para esto en Node.js usando expresiones regulares?
let convertedObj = JSON.stringify(obj); const fullRegex = /(url|Url)":"([^"]*)/g; const fullMatch = convertedObj.match(fullRegex); if(fullMatch) { fullMatch.forEach((str, i) => { convertedObj = convertedObj.replace(str, `${str}?d=1234`); }); } console.log(convertedObj);