I am working with a Discord bot and have a JSON file called config.json that looks like this:
{
"token": "stuff"
"prefix": "!"
}
And I want to replace the "prefix: "!" line. My code is this:
if(cmd == "prefix"){
var new_prefix = "\"prefix\": " + "\"" + String(args[0]) + "\"";
var data = fs.readFileSync("config.json", "utf-8");
var newValue = data.replace(/"prefix"\s*:\s*".+"/gm, "new_prefix");
fs.writeFileSync("config.json", new_prefix, "utf-8");
};
Instead of only replacing the one line, it overwrites my entire config.JSON file so that after it looks like this:
"prefix":"stuffHere"
How can I make it only replace the one line and left the rest of the file intact?