const regex = /\[((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\]/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\[((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\]', 'g')
const str = `Nov 16 14:21:05 192.168.77.20/192.168.77.167 1 2016-11-16T14:21:05+08:00 s_sys@aaa1 snmptrapd 19644 - - '14:21:5 16/11/2016 UDP: [10.80.0.254]:58919 wcore Cold Start sysUpTimeInstance = 24:16:28:17.07 snmpTrapOID.0 = cmnMacMoveNotification cmnMACMoveAddress.0 = e0:66:78:8a:93:bb cmnMACMoveVlanNumber.0 = 30 cmnMACMoveFromPortId.0 = 1669 cmnMACMoveToPortId.0 = 1668 cmnMACMoveTime.0 = 24:16:28:17.07'`;
// 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