Some “ObjectId” is not getting replaced












-2















I am converting the mongodb's document _id

from : "_id" : ObjectId("5c09b6002351d50e100c5f6c"),

To: "_id" : "5c09b6002351d50e100c5f6c",


I am using the regex text.replace(/ObjectId((.*))/gi,"$1");



Issue is some ObjectId gets converted but some doesn't gets converted,don't know why..



small example code of what i am doing:



var fs=require('fs');
var data = fs.readFileSync('./abc.json');
str = data.toString()
str = str.replace(/ObjectId((.*))/g,"$1");
fs.writeFile('str','data.json');


note: I have exported the input file using Studio 3T



Thanks in advance!










share|improve this question

























  • You have a problem, then you used regex and now you have two problems.

    – cgTag
    Jan 2 at 17:43











  • you mean to say the input file itself has an issue? @cgTag

    – Prabhat Mishra
    Jan 2 at 17:45











  • No, it's an old saying :) blog.codinghorror.com/…

    – cgTag
    Jan 2 at 17:54


















-2















I am converting the mongodb's document _id

from : "_id" : ObjectId("5c09b6002351d50e100c5f6c"),

To: "_id" : "5c09b6002351d50e100c5f6c",


I am using the regex text.replace(/ObjectId((.*))/gi,"$1");



Issue is some ObjectId gets converted but some doesn't gets converted,don't know why..



small example code of what i am doing:



var fs=require('fs');
var data = fs.readFileSync('./abc.json');
str = data.toString()
str = str.replace(/ObjectId((.*))/g,"$1");
fs.writeFile('str','data.json');


note: I have exported the input file using Studio 3T



Thanks in advance!










share|improve this question

























  • You have a problem, then you used regex and now you have two problems.

    – cgTag
    Jan 2 at 17:43











  • you mean to say the input file itself has an issue? @cgTag

    – Prabhat Mishra
    Jan 2 at 17:45











  • No, it's an old saying :) blog.codinghorror.com/…

    – cgTag
    Jan 2 at 17:54
















-2












-2








-2


0






I am converting the mongodb's document _id

from : "_id" : ObjectId("5c09b6002351d50e100c5f6c"),

To: "_id" : "5c09b6002351d50e100c5f6c",


I am using the regex text.replace(/ObjectId((.*))/gi,"$1");



Issue is some ObjectId gets converted but some doesn't gets converted,don't know why..



small example code of what i am doing:



var fs=require('fs');
var data = fs.readFileSync('./abc.json');
str = data.toString()
str = str.replace(/ObjectId((.*))/g,"$1");
fs.writeFile('str','data.json');


note: I have exported the input file using Studio 3T



Thanks in advance!










share|improve this question
















I am converting the mongodb's document _id

from : "_id" : ObjectId("5c09b6002351d50e100c5f6c"),

To: "_id" : "5c09b6002351d50e100c5f6c",


I am using the regex text.replace(/ObjectId((.*))/gi,"$1");



Issue is some ObjectId gets converted but some doesn't gets converted,don't know why..



small example code of what i am doing:



var fs=require('fs');
var data = fs.readFileSync('./abc.json');
str = data.toString()
str = str.replace(/ObjectId((.*))/g,"$1");
fs.writeFile('str','data.json');


note: I have exported the input file using Studio 3T



Thanks in advance!







javascript node.js regex mongodb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 20:02







Prabhat Mishra

















asked Jan 2 at 17:36









Prabhat MishraPrabhat Mishra

11317




11317













  • You have a problem, then you used regex and now you have two problems.

    – cgTag
    Jan 2 at 17:43











  • you mean to say the input file itself has an issue? @cgTag

    – Prabhat Mishra
    Jan 2 at 17:45











  • No, it's an old saying :) blog.codinghorror.com/…

    – cgTag
    Jan 2 at 17:54





















  • You have a problem, then you used regex and now you have two problems.

    – cgTag
    Jan 2 at 17:43











  • you mean to say the input file itself has an issue? @cgTag

    – Prabhat Mishra
    Jan 2 at 17:45











  • No, it's an old saying :) blog.codinghorror.com/…

    – cgTag
    Jan 2 at 17:54



















You have a problem, then you used regex and now you have two problems.

– cgTag
Jan 2 at 17:43





You have a problem, then you used regex and now you have two problems.

– cgTag
Jan 2 at 17:43













you mean to say the input file itself has an issue? @cgTag

– Prabhat Mishra
Jan 2 at 17:45





you mean to say the input file itself has an issue? @cgTag

– Prabhat Mishra
Jan 2 at 17:45













No, it's an old saying :) blog.codinghorror.com/…

– cgTag
Jan 2 at 17:54







No, it's an old saying :) blog.codinghorror.com/…

– cgTag
Jan 2 at 17:54














1 Answer
1






active

oldest

votes


















0














The problem is your .* also matches some whole "ObjectId(...)" expressions.



A solution is to use a non greedy group:



text.replace(/ObjectId((.*?))/gi,"$1");


A better (more performant) one is to make it explicit if you can:



text.replace(/ObjectId(("[^"]*"))/gi,"$1");





share|improve this answer
























  • no change still it's replacing only some instances...

    – Prabhat Mishra
    Jan 2 at 17:44











  • @PrabhatMishra can you build a MCVE please?

    – Denys Séguret
    Jan 2 at 17:45













  • pls check i have edited the question @Denys

    – Prabhat Mishra
    Jan 2 at 17:51











  • @PrabhatMishra yes but still no way to check or reproduce your problem.

    – Denys Séguret
    Jan 2 at 17:57











  • i have exported this file using Studio 3T you can try that to reproduce the problem..@Denys

    – Prabhat Mishra
    Jan 2 at 18:00












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010740%2fsome-objectid-is-not-getting-replaced%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The problem is your .* also matches some whole "ObjectId(...)" expressions.



A solution is to use a non greedy group:



text.replace(/ObjectId((.*?))/gi,"$1");


A better (more performant) one is to make it explicit if you can:



text.replace(/ObjectId(("[^"]*"))/gi,"$1");





share|improve this answer
























  • no change still it's replacing only some instances...

    – Prabhat Mishra
    Jan 2 at 17:44











  • @PrabhatMishra can you build a MCVE please?

    – Denys Séguret
    Jan 2 at 17:45













  • pls check i have edited the question @Denys

    – Prabhat Mishra
    Jan 2 at 17:51











  • @PrabhatMishra yes but still no way to check or reproduce your problem.

    – Denys Séguret
    Jan 2 at 17:57











  • i have exported this file using Studio 3T you can try that to reproduce the problem..@Denys

    – Prabhat Mishra
    Jan 2 at 18:00
















0














The problem is your .* also matches some whole "ObjectId(...)" expressions.



A solution is to use a non greedy group:



text.replace(/ObjectId((.*?))/gi,"$1");


A better (more performant) one is to make it explicit if you can:



text.replace(/ObjectId(("[^"]*"))/gi,"$1");





share|improve this answer
























  • no change still it's replacing only some instances...

    – Prabhat Mishra
    Jan 2 at 17:44











  • @PrabhatMishra can you build a MCVE please?

    – Denys Séguret
    Jan 2 at 17:45













  • pls check i have edited the question @Denys

    – Prabhat Mishra
    Jan 2 at 17:51











  • @PrabhatMishra yes but still no way to check or reproduce your problem.

    – Denys Séguret
    Jan 2 at 17:57











  • i have exported this file using Studio 3T you can try that to reproduce the problem..@Denys

    – Prabhat Mishra
    Jan 2 at 18:00














0












0








0







The problem is your .* also matches some whole "ObjectId(...)" expressions.



A solution is to use a non greedy group:



text.replace(/ObjectId((.*?))/gi,"$1");


A better (more performant) one is to make it explicit if you can:



text.replace(/ObjectId(("[^"]*"))/gi,"$1");





share|improve this answer













The problem is your .* also matches some whole "ObjectId(...)" expressions.



A solution is to use a non greedy group:



text.replace(/ObjectId((.*?))/gi,"$1");


A better (more performant) one is to make it explicit if you can:



text.replace(/ObjectId(("[^"]*"))/gi,"$1");






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 17:38









Denys SéguretDenys Séguret

281k56589605




281k56589605













  • no change still it's replacing only some instances...

    – Prabhat Mishra
    Jan 2 at 17:44











  • @PrabhatMishra can you build a MCVE please?

    – Denys Séguret
    Jan 2 at 17:45













  • pls check i have edited the question @Denys

    – Prabhat Mishra
    Jan 2 at 17:51











  • @PrabhatMishra yes but still no way to check or reproduce your problem.

    – Denys Séguret
    Jan 2 at 17:57











  • i have exported this file using Studio 3T you can try that to reproduce the problem..@Denys

    – Prabhat Mishra
    Jan 2 at 18:00



















  • no change still it's replacing only some instances...

    – Prabhat Mishra
    Jan 2 at 17:44











  • @PrabhatMishra can you build a MCVE please?

    – Denys Séguret
    Jan 2 at 17:45













  • pls check i have edited the question @Denys

    – Prabhat Mishra
    Jan 2 at 17:51











  • @PrabhatMishra yes but still no way to check or reproduce your problem.

    – Denys Séguret
    Jan 2 at 17:57











  • i have exported this file using Studio 3T you can try that to reproduce the problem..@Denys

    – Prabhat Mishra
    Jan 2 at 18:00

















no change still it's replacing only some instances...

– Prabhat Mishra
Jan 2 at 17:44





no change still it's replacing only some instances...

– Prabhat Mishra
Jan 2 at 17:44













@PrabhatMishra can you build a MCVE please?

– Denys Séguret
Jan 2 at 17:45







@PrabhatMishra can you build a MCVE please?

– Denys Séguret
Jan 2 at 17:45















pls check i have edited the question @Denys

– Prabhat Mishra
Jan 2 at 17:51





pls check i have edited the question @Denys

– Prabhat Mishra
Jan 2 at 17:51













@PrabhatMishra yes but still no way to check or reproduce your problem.

– Denys Séguret
Jan 2 at 17:57





@PrabhatMishra yes but still no way to check or reproduce your problem.

– Denys Séguret
Jan 2 at 17:57













i have exported this file using Studio 3T you can try that to reproduce the problem..@Denys

– Prabhat Mishra
Jan 2 at 18:00





i have exported this file using Studio 3T you can try that to reproduce the problem..@Denys

– Prabhat Mishra
Jan 2 at 18:00




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010740%2fsome-objectid-is-not-getting-replaced%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter