I need to extract JSON string (which will be used later to parse JSON) from a log file. The file is in this format:
[16:11:20] some text
[16:11:20] some text
some text
some text
[
{
"description": "some text",
"elements": [{
"id": "some text",
"keyword": "some text",
"line": 20,
"name": "some text",
"steps": [{
"arguments": [],
"keyword": "some text ",
"result": {
"status": "passed",
"duration": 14884761888
},
"hidden": true,
"match": {
"location": "some text"
}
},
{
"arguments": [],
"keyword": "sometext ",
"name": "sometext",
"result": {
"status": "passed",
"duration": 463674
},
"line": 11,
"match": {
"location": "sometext"
}
}
],
"tags": [
{
"name": "@sometext-no",
"line": 7
},
{
"name": "@sometext",
"line": 8
}
],
"type": "sometext"
}
],
"id": "sometext",
"keyword": "sometext",
"line": 1,
"name": "sometext",
"tags": [],
"uri": "sometext"
}
][16:11:54] some text
[16:11:54] some text
How can I construct a regex in JavaScript which will extract JSON data from this file so that it can be used by string.match() to give the desired output
The pattern I am trying is as below
var regExtract = /(\[\s+\{[\s\S]*\}\s+\]\s+\}\s+\]\s+\}\s+\])/;
var matchedJson = data.toString().match(regExtract)[1];
\s+means "one or more whitespace characters", so that means your pattern will not match your example. You also have the end of your pattern,}]several times.