I have some texts formatted in this way:
var string = 'some random text fjijd {"Type":"NewTunnel","Payload":{"ReqId":"e425ebb50d220816","Url":"asaa","Protocol":"http","Error":""}} some other text bla bla'
I am wondering how can i extract the json stringified object that can be parsed then with a regex.
I am running a sub process from node.js application using spawn
and the stdout
data (that is application log -log=stdout
) in plain text formatted in this way. Don't ask me how and why, sorry!
var cproc = require("child_process");
proc = cproc.spawn("./ngrok", ['-log=stdout', '8080']);
proc.stdout.on("data", function(res) {
console.log("Data received: " + res);
});
Data received is plain text, something like this:
[08/10/14 01:49:37] [DEBG] [ctl:604a481c] Read message {"Type":"ReqProxy","Payload":{}}
It includes also debugging infos!
Last update: yet another solution
/((?:{).*(?:}))/g
The initial answer:
What about this. I believe you need only the JSON part within this string. If you don't expect nested object the pattern could be changed to /({.*?})/g
var s = 'some random text fjijd {"Type":"NewTunnel","Payload":{"ReqId":"e425ebb50d220816","Url":"asaa","Protocol":"http","Error":""}} some other text bla bla';
var m = s.match(/({.*})/g); // or /({+.*}+)/g or /(({+).*(}+))/g
console.log(m[0]);
Output:
{"Type":"NewTunnel","Payload":{"ReqId":"e425ebb50d220816","Url":"asaa","Protocol":"http","Error":""}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.