const regex = /CHARGETYPE\s\[(?<CHARGETYPE>[^\]]*)]/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('CHARGETYPE\\s\\[(?<CHARGETYPE>[^\\]]*)]', 'gm')
const str = `2023-04-25 23:15:00.438, BUSINESSIDENTIFIER="06-02-10112022-85346-L", MESSAGEIDENTIFIER="42920d7b-4bde-4a7c-9704-532bc178acfd", PAYLOAD="BusinessIdentifier : 06-02-10112022-85346-L ***** MessageIdentifier : 42920d7b-4bde-4a7c-9704-532bc178acfd ***** TimeStamp : 2023-04-25T23:00:48.149+08:00 ***** ElapsedTime : 0.05 ***** InterfaceName : BRM ***** ServiceLayerName : Remedy ***** ServiceLayerOperation : CreateQPBillingEvents ***** ServiceLayerPipeline : ServiceLayerErrorHandler ***** SiteID : AWS ***** DomainName : OSBDomain ***** ServerName : OSBServer ***** FusionErrorCode : ***** FusionErrorMessage : ***** <ns2:createQPBillEventsResponse xmlns:ns2="com.alcatel.lucent.on.ws.manager"><reason>Insert into tables failed-ORA-00001:Duplicate Check Fail for ORDER_ID[06-02-10112022-85346-L] TROUBLE_TICKET_ID[] CHARGETYPE [RSCN4]</reason><response_Code>-1</response_Code></ns2:createQPBillEventsResponse>", TIMESTAMP="2023-04-25 23:00:48.149", SERVICELAYEROPERATION="CreateQPBillingEvents", ELAPSEDTIME="0.05", SERVICELAYERPIPELINE="ServiceLayerErrorHandler", REASON="Insert into tables failed-ORA-00001:Duplicate Check Fail for ORDER_ID[06-02-10112022-85346-L] TROUBLE_TICKET_ID[] CHARGETYPE [RSCN4]", RESPONSECODE="-1"`;
// 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