-1

I have a mongodb query that may contain dynamic values for example

[{$group: { _id: '$os_name', count: { $sum: 1 } }},{$sort:{count:-1}},{$out:'newc'},{$group: { _id: '$os_name', count: { $sum: 1 } }}]
[{$group: { _id: '$browser', count: { $sum: 1 } }},{$sort:{count:-1}},{$out:'deffC'}]

I want always match everything without ,{$out:'anything'}

End result is

[{$group: { _id: '$os_name', count: { $sum: 1 } }},{$sort:{count:-1}},{$group: { _id: '$os_name', count: { $sum: 1 } }}]
[{$group: { _id: '$browser', count: { $sum: 1 } }},{$sort:{count:-1}}]
Holger Just
  • 52,918
  • 14
  • 115
  • 123
khaled
  • 3
  • 2
  • [You should not parse JSON with a regex](https://stackoverflow.com/a/1758162/6320039) (this is for XML, but applies for json) – Ulysse BN Aug 14 '17 at 17:42

1 Answers1

0

Your regex to match that string is this: /,\{\$out:'.*'\}/g

let s = `[{$group: { _id: '$os_name', count: { $sum: 1 } }},{$sort:{count:-1}},{$out:'newc'},{$group: { _id: '$os_name', count: { $sum: 1 } }}]
         [{$group: { _id: '$browser', count: { $sum: 1 } }},{$sort:{count:-1}},{$out:'deffC'}]`;

s = s.replace(/,\{\$out:'.*'\}/g, '');

console.log(s);
jdubjdub
  • 613
  • 7
  • 21