2017-08-14 149 views
-1

我有一個MongoDB的查詢例如可以包含動態值在正則表達式的Javascript不匹配特定模式

[{$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'}] 

我想總是一致的一切,而不,{$out:'anything'}

最終的結果是

[{$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}}] 
+0

[你不應該用正則表達式解析JSON(https://stackoverflow.com/a/1758162/6320039)(這是XML,但適用於JSON) –

回答

0

您的正則表達式匹配該字符串是這樣的:/,\{\$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);