const regex = /(NAME)((\s)?:(\s)?)([ |,| (|)|\\|\.|\"|\w|\-|\+]*)((\s)*?,(\s)*?)(DESCR)((\s)?:(\s)?)([ |,| (|)|\\|\.|\"|\w|\-|\+]*)(\s)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(NAME)((\\s)?:(\\s)?)([ |,| (|)|\\\\|\\.|\\"|\\w|\\-|\\+]*)((\\s)*?,(\\s)*?)(DESCR)((\\s)?:(\\s)?)([ |,| (|)|\\\\|\\.|\\"|\\w|\\-|\\+]*)(\\s)', 'g')
const str = `WDL3-4N-C4510-1A#show inventory
NAME: "Switch System", DESCR: "Cisco Systems, Inc. WS-C4510R+E 10 slot switch "
PID: WS-C4510R+E , VID: V03 , SN: FOX1616GNS8
NAME: "Clock Module", DESCR: "Clock Module"
PID: WS-X4K-CLOCK-E , VID: V01 , SN: NWG161794PT
NAME: "Mux Buffer 1 ", DESCR: "Mux Buffers for Redundancy Logic"
PID: WS-X4590-EX= , VID: , SN: NWG16179CP9
NAME: "Mux Buffer 2 ", DESCR: "Mux Buffers for Redundancy Logic"
PID: WS-X4590-EX= , VID: , SN: NWG16179C3T
NAME: "Mux Buffer 3 ", DESCR: "Mux Buffers for Redundancy Logic"
PID: WS-X4590-EX= , VID: , SN: NWG16179CUN
NAME: "Mux Buffer 4 ", DESCR: "Mux Buffers for Redundancy Logic"
PID: WS-X4590-EX= , VID: , SN: NWG16179AXZ
NAME: "Mux Buffer 7 ", DESCR: "Mux Buffers for Redundancy Logic"
PID: WS-X4590-EX= , VID: , SN: NWG16179CTE
NAME: "Mux Buffer 8 ", DESCR: "Mux Buffers for Redundancy Logic"
PID: WS-X4590-EX= , VID: , SN: NWG16179C3V
NAME: "Mux Buffer 9 ", DESCR: "Mux Buffers for Redundancy Logic"
PID: WS-X4590-EX= , VID: , SN: NWG16179CLW
NAME: "Mux Buffer 10 ", DESCR: "Mux Buffers for Redundancy Logic"
PID: WS-X4590-EX= , VID: , SN: NWG16179CVU
NAME: "Linecard(slot 1)", DESCR: "10/100/1000BaseT (RJ45)+V E Series with 48 10/100/1000 baseT Premium PoE ports (Cisco/IEEE)"
PID: WS-X4748-RJ45V+E , VID: V03 , SN: CAT1624L2J7
`;
// 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