const regex = /(?ms)^(?<date>\w+\s\d+)\s+(?<Time>\d+:\d+:\d+).*defaultMsg\":\"(?<defaultMsg>[^\"]+).*userName\":\"(?<userName>[^\"]+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?ms)^(?<date>\\w+\\s\\d+)\\s+(?<Time>\\d+:\\d+:\\d+).*defaultMsg\\":\\"(?<defaultMsg>[^\\"]+).*userName\\":\\"(?<userName>[^\\"]+)', 'gm')
const str = `Aug 25 17:41:951 IP_ADDR consolidated_audit: {"affectedEntityList": [{"entityType":"vm","name":"hostname | serverpurpose","uuid":"Ilaef477-6f03-4a22-baa7-23736c8e741c"}],"alertUid":"VmUpdateAudit","classificationList":["UserAction"],"creationTimestampUsecs": "1629874233889978",["defaultMsg":"Updated VM hostname serverpurpose","opEndTimestampUsecs","1629874233884373","opStartTimestampUsecs","1629874233779524","operationType";"Update","originatingClusterUuid":"0005ad50-f430-a46c-001e-e4434bb76b0e","params":{"is_secure_boot":"false","is_uefi_boot":"true","vm_name":"hostname | serverpurpose"}, "recordType": "Audit","severity":"Audit","userName":"abc@abc.com","userUuid":"3f73d040-7516-5caa-a956-91179b2cblf5","uuid":"254f3952-bf9b-4205-8al4-ad2074ed70ab"}`;
// 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