const regex = /^(?P<hostname>[\w-_]+)\s+(?P<localInterface>[A-Za-z]+\d+(?:\/\d+)*)\s+(?P<holdTime>\d+)\s+(?P<capabilities>[A-Z]+)\s+(?P<remoteInterface>[A-Za-z]+\d+(?:\/\d+)*) /gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?P<hostname>[\\w-_]+)\\s+(?P<localInterface>[A-Za-z]+\\d+(?:\\\/\\d+)*)\\s+(?P<holdTime>\\d+)\\s+(?P<capabilities>[A-Z]+)\\s+(?P<remoteInterface>[A-Za-z]+\\d+(?:\\\/\\d+)*) ', 'gm')
const str = `sh cdp nei
Capability Codes: R - Router, T - Trans-Bridge, B - Source-Route-Bridge
S - Switch, H - Host, I - IGMP, r - Repeater,
V - VoIP-Phone, D - Remotely-Managed-Device,
s - Supports-STP-Dispute
Device-ID Local Intrfce Hldtme Capability Platform Port ID
CHOD-MGMT-STACK.mhmp
mgmt0 129 S I WS-C2960X-48T Gig1/0/25
SPINE-401(FDO22041YMP)
Eth1/49 153 R S s N9K-C9336PQ Eth1/36
SPINE-402(FDO22081W5Y)
Eth1/50 176 R S s N9K-C9336PQ Eth1/36
IPN-2-CH(FDO221610MG)
Eth1/51 125 R S s N9K-C93180YC- Eth1/51
IPN-2-CH(FDO221610MG)
Eth1/52 125 R S s N9K-C93180YC- Eth1/52
IPN-1-KCP(FDO22152M53)
Eth1/54 143 R S s N9K-C93180YC- Eth1/54
Total entries displayed: 6
IPN-1-CH# sh lldp nei
Capability codes:
(R) Router, (B) Bridge, (T) Telephone, (C) DOCSIS Cable Device
(W) WLAN Access Point, (P) Repeater, (S) Station, (O) Other
Device ID Local Intf Hold-time Capability Port ID
SPINE-401 Eth1/49 120 BR Eth1/36
SPINE-402 Eth1/50 120 BR Eth1/36
IPN-2-CH Eth1/51 120 BR Ethernet1/51
IPN-2-CH Eth1/52 120 BR Ethernet1/52
IPN-1-KCP Eth1/54 120 BR Ethernet1/54
Total entries displayed: 5`;
// 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