const regex = /(\d{1,2}\: (.+)\: \<(.+)\>.*mtu (\d+).*state (UP|DOWN|UNKNOWN).*\n.*|\n.*inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/\d{1,2})/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(\\d{1,2}\\: (.+)\\: \\<(.+)\\>.*mtu (\\d+).*state (UP|DOWN|UNKNOWN).*\\n.*|\\n.*inet (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\\/\\d{1,2})', 'g')
const str = `1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether b8:27:eb:30:65:42 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:65:30:17 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.22/24 brd 192.168.178.255 scope global dynamic noprefixroute wlan0
valid_lft 861425sec preferred_lft 753425sec
inet6 fe80::d32b:df6a:d0e1:d925/64 scope link
valid_lft forever preferred_lft forever`;
// 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