const regex = /{"name":"[^"]*","id":"([^"]*)","type":"([^"]*)","available":true,[^{}]*}/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('{"name":"[^"]*","id":"([^"]*)","type":"([^"]*)","available":true,[^{}]*}', 'g')
const str = `{"error":null,"data":{"airports":[],"pocs":[],"regions":[{"name":"Central America","id":"L04305","type":"CruiseArea","available":false,"countries":"Mexico","group":null},{"name":"Caribbean","id":"L04304","type":"CruiseArea","available":false,"countries":"St Maarten,Barbados,British Virgin Islands,St Kitts and Nevis,St Vincent and the Grenadines,Antigua","group":null},{"name":"Western Mediterranean","id":"L34381","type":"CruiseArea","available":true,"countries":"Spain","group":null},{"name":"Eastern Mediterranean","id":"L34373","type":"CruiseArea","available":false,"countries":"Greece,Italy,Slovenia,Montenegro,Croatia","group":null},{"name":"North Africa And Middle East","id":"L04301","type":"CruiseArea","available":false,"countries":"Morocco","group":null},{"name":"Fjords, Iceland And Arctic","id":"L34384","type":"CruiseArea","available":true,"countries":"Norway","group":null},{"name":"Northern Europe And UK","id":"L34383","type":"CruiseArea","available":true,"countries":"United Kingdom","group":null}],"countries":[{"name":"Spain","id":"ESP","type":"COUNTRY","available":true,"countries":null,"group":null},{"name":"Jamaica","id":"JAM","type":"COUNTRY","available":false,"countries":null,"group":null},{"name":"Greece","id":"GRC","type":"COUNTRY","available":false,"countries":null,"group":null},{"name":"Italy","id":"ITA","type":"COUNTRY","available":false,"countries":null,"group":null},{"name":"Turkey","id":"TUR","type":"COUNTRY","available":false,"countries":null,"group":null},{"name":"Barbados","id":"BRB","type":"COUNTRY","available":false,"countries":null,"group":null},{"name":"Mexico","id":"MEX","type":"COUNTRY","available":false,"countries":null,"group":null},{"name":"Israel","id":"ISR","type":"COUNTRY","available":false,"countries":null,"group":null},{"name":"Cuba","id":"CUB","type":"COUNTRY","available":false,"countries":null,"group":null},{"name":"Croatia","id":"HRV","type":"COUNTRY","available":false,"countries":null,"group":null}],"itineraries":[],"airportGroups":[]},"nomatch":false,"durations":null,"searchError":null}`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions