while executing the following code:
let queryStr = JSON.stringify(queryObj);
console.log(queryStr);
queryStr = queryStr.replace(/\b(gte|gt|lte|lt)\b/g, (match) => {
`$${match}`;
console.log(queryStr);
});
I receive the following results from 'console.log':
{"price{gt}":"1500"}
{"price{gt}":"1500"}
http://127.0.0.1:3000/products?price{gt}=1500Why does the replace not work? Could you help me, please?
Your code basically worked, but there were some inaccuracies. Mainly, your replace function didn't return anything, and the second log statement was in the wrong place/executed before you wanted it to be executed.
const queryObj = {"price{gt}":"1500"};
const input = JSON.stringify(queryObj);
console.log("input:", input);
const queryStr = input.replace(/\b(gte|gt|lte|lt)\b/g, (match) => `$${match}`);
console.log("queryStr:", queryStr);