I have a function in Javascript to match text between curly braces (including the braces) in order to extract a JSON string from different posts. The function is below:
function eventObject(value) {
var json = text.match(/{([^}]+)}/)[0]; // matches content between curly braces
return json;
}
The input looks like this:
{
"host": "Host link..",
"info": "Info text...",
"links": [ {"title": "link_title", "url": "link_url"}, {"title": "link_title_2", "url": "link_url_2"} ],
"category": "campfire"
}
The problem is this text contains a nested string with more curly brackets. The output cuts as soon as it gets to the first occurrence of a closing bracket in the links. How can I prevent this from happening in order to get the full string?
Update
I realised I left out some important information to simplify my question: the API response is a string of raw html that contains the string I would like to parse as an object. The typical raw HTML looks like this:
"cooked":"<pre><code class=\"lang-auto\">{\n\"host\": \"host_link\",\n\"info\": \"event_info\",\n\"links\": [{\"title\": \"link_title \", \"url\": \"link_url"},{\"title\": \"link_two_title \", \"url\": \"link_two_url\"} ],\n\"category\": \"category\"\n}\n</code></pre>"}
The challenge is extracting the entire string between <code></code>
and parsing it into an object. I have updated the title of the question to reflect this.
The following function successfully extracts the string and strips the html tags and line breaks, but it does not correctly parse it as an object:
function eventObject(value){
const doc = new DOMParser().parseFromString(value, "text/html");
var json = [...doc.querySelectorAll('code')].map(code => code.textContent); // DOMParser extracts text between <code> tags
var final = String(json).replace(/\n/g, " ").replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"'); // removes line breaks and replaces curly quotes with straight quotes
var string = JSON.stringify(final);
var obj = JSON.parse("'" + string + "'");
return obj;
}