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"}
- This is my get query:
http://127.0.0.1:3000/products?price{gt}=1500
Why does the replace not work? Could you help me, please?
CodePudding user response:
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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>