const regex = new RegExp('(?P<interface>\\w+(?:\\d/?)+(?:[\\.]\\d{1,3})?)\\s+(?P<ip_address>(?:\\w[\\.]?)+)\\s+(?P<ok>\\w+)\\s+(?P<method>\\w+)\\s+(?P<status>(?:\\w+\\s)?[\\w]+)\\s+(?P<protocol>\\w+)', 'gm')
const str = `Router# show ip interface brief
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/1 unassigned YES unset up up
GigabitEthernet0/2.1 192.168.190.235 YES unset up up
GigabitEthernet0/3 unassigned YES unset up up
GigabitEthernet0/4 192.168.191.2 YES unset up up
TenGigabitEthernet2/1 unassigned YES unset up up
TenGigabitEthernet2/2 unassigned YES unset up up
TenGigabitEthernet2/3 unassigned YES unset up up
TenGigabitEthernet2/4 unassigned YES unset down down
GigabitEthernet36/1 unassigned YES unset down down
GigabitEthernet36/2 unassigned YES unset down down
GigabitEthernet36/11 unassigned YES unset administratively down down
GigabitEthernet36/25 unassigned YES unset down down
Te36/45 unassigned YES unset down down
Te36/46 unassigned YES unset down down
Te36/47 unassigned YES unset down down
Te36/48 unassigned YES unset down down
Virtual36 unassigned YES unset up up`;
// 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