const regex = /^ ([\w-_ ]+):\n\n +Type: Ethernet\n +Hardware.+\n +BSD Device Name: (.+)\n +IPv4 Addresses: (192[\.\d]+)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^ ([\\w-_ ]+):\\n\\n +Type: Ethernet\\n +Hardware.+\\n +BSD Device Name: (.+)\\n +IPv4 Addresses: (192[\\.\\d]+)$', 'gm')
const str = `ETHERNET CONNECTED, NO WI-FI
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Below Source Text from Mac Shell Script:
system_profiler SPNetworkDataType
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Network:
Thunderbolt Ethernet Slot 1:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en8
IPv4 Addresses: 192.168.1.15
IPv4:
AdditionalRoutes:
DestinationAddress: 192.168.1.15
SubnetMask: 255.255.255.255
DestinationAddress: 169.254.0.0
SubnetMask: 255.255.0.0
Addresses: 192.168.1.15
ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69
ARPResolvedIPAddress: 192.168.1.1
Configuration Method: DHCP
ConfirmedInterfaceName: en8
Interface Name: en8
Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69
Router: 192.168.1.1
Subnet Masks: 255.255.255.0
IPv6:
Configuration Method: Automatic
DNS:
Server Addresses: 192.168.1.1
DHCP Server Responses:
Domain Name Servers: 192.168.1.1
Lease Duration (seconds): 0
DHCP Message Type: 0x05
Routers: 192.168.1.1
Server Identifier: 192.168.1.1
Subnet Mask: 255.255.255.0
Ethernet:
MAC Address: 00:50:b6:ca:f0:dc
Media Options: Full Duplex, Flow Control
Media Subtype: 1000baseT
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Sleep Proxies:
1 Family Room Apple TV:
Marginal Power: 60
Metric: 703560
Portability: 35
Total Power: 63
Type: 70
Service Order: 0
Wi-Fi:
Type: AirPort
Hardware: AirPort
BSD Device Name: en0
IPv4 Addresses: 192.168.1.16
IPv4:
AdditionalRoutes:
DestinationAddress: 192.168.1.16
SubnetMask: 255.255.255.255
DestinationAddress: 169.254.0.0
SubnetMask: 255.255.0.0
Addresses: 192.168.1.16
ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69
ARPResolvedIPAddress: 192.168.1.1
Configuration Method: DHCP
ConfirmedInterfaceName: en0
Interface Name: en0
Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69
Router: 192.168.1.1
Subnet Masks: 255.255.255.0
IPv6:
Configuration Method: Automatic
DNS:
Server Addresses: 192.168.1.1
DHCP Server Responses:
Domain Name Servers: 192.168.1.1
Lease Duration (seconds): 0
DHCP Message Type: 0x05
Routers: 192.168.1.1
Server Identifier: 192.168.1.1
Subnet Mask: 255.255.255.0
Ethernet:
MAC Address: 28:cf:e9:17:e0:d1
Media Options:
Media Subtype: Auto Select
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 2
Thunderbolt Ethernet:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en6
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 1
Wi-Fi:
Type: AirPort
Hardware: AirPort
BSD Device Name: en0
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Ethernet:
MAC Address: 28:cf:e9:17:e0:d1
Media Options:
Media Subtype: Auto Select
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 2
USB Ethernet:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en3
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 3
AX88179 USB 3.0 to Gigabit Ethernet:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en7
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 4
Thunderbolt FireWire:
Type: FireWire
Hardware: FireWire
BSD Device Name: fw0
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Ethernet:
MAC Address: 00:50:b6:10:00:00:5d:31
Media Options: Full Duplex
Media Subtype: Auto Select
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 5
iPhone:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en5
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 6
Bluetooth PAN:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en4
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 7
~~~~~~~~~~~~~~~~~~~~~~~~
Type: Ethernet\\n +Hardware.+\\n +BSD.+\\n +IPv4 Addresses: 192[\\.\\d]+\$`;
// 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