const regex = /(?s)json:(?!.*json:.*totalCount.*).*?totalCount.*?(?=\[2)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?s)json:(?!.*json:.*totalCount.*).*?totalCount.*?(?=\\[2)', 'gm')
const str = `[2018-12-10 06:30:44]JSON回调处理--->>> 同步运动数据 [2018-12-10 06:30:44]同步运动健康数据json:{ "day" : 10, "items" : [ { "activeTime" : 5, "calory" : 0, "distance" : 0, "stepCount" : 0 },
{ "activeTime" : 0, "calory" : 0, "distance" : 0, "stepCount" : 0 }, { "activeTime" : 0, "calory" : 0, "distance" : 0, "stepCount" : 0 } ], "month" : 12, "startTime" : 0, "timeSpace" : 15, "totalActiveTime" : 81, "totalCalory" : 0, "totalDistance" : 0, "totalCount" : 0, "year" : 2018 }
[2018-12-10 06:30:44]同步运动健康数据items.sise()=96
[2018-12-10 06:30:38]..再同步健康数据...
[2018-12-10 06:30:44]JSON回调处理--->>> 同步运动数据 [2018-12-10 06:30:44]同步运动健康数据json:{ "day" : 10, "items" : [ { "activeTime" : 5, "calory" : 0, "distance" : 0, "stepCount" : 0 },
{ "activeTime" : 0, "calory" : 0, "distance" : 0, "stepCount" : 0 }, { "activeTime" : 0, "calory" : 0, "distance" : 0, "stepCount" : 0 } ], "month" : 12, "startTime" : 0,
"timeSpace" : 15, "totalActiveTime" : 81, "totalCalory" : 0, "totalDistance" : 0, "StepCount" : 0, "year" : 2018 }
[2018-12-10 06:30:44]同步运动健康数据items.sise()=96
[2018-12-10 06:30:38]..再同步健康数据... `;
// 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