const regex = /^config (dhcp|dns|router|interface) (?:ip|ipv4|id|bgp|name) (<[^>]*>|(?:\d+\.){3}\d+|[a-z]+ id \d+|\d+)( port| timeout| id|\s*)( <[^>]*>\s*| \d+\s*|\s*)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^config (dhcp|dns|router|interface) (?:ip|ipv4|id|bgp|name) (<[^>]*>|(?:\\d+\\.){3}\\d+|[a-z]+ id \\d+|\\d+)( port| timeout| id|\\s*)( <[^>]*>\\s*| \\d+\\s*|\\s*)$', 'gm')
const str = `config dhcp ip <dhcp-ipaddress> port <dhcp-port-number>
config dhcp ip <dhcp-ipaddress> timeout <time-out-value>
config dhcp ipv4 <dhcp-ipaddress>
config dns ip <dns-ipaddress> port <dns-port-number>
config dns ip <dns-ipaddress> timeout <time-out-value>
config router bgp <bgp-number>
config interface id <interface-id>
config interface name <interface-name> id <interface-id>
config dhcp ip 1.1.1.1 port 8080
config dhcp ip 1.1.1.2 timeout 120
config dhcp ip 1.1.1.1 timeout 120
config dhcp ip 1.1.1.2 port 8080
config dhcp ipv4 1.1.1.3
config interface id 12
config interface name abc id 12
config interface id 13
config interface name xyz id 13
config dhcp ip 1.1.1.1 port 8080
config dhcp ip 1.1.1.1 timeout 120
config dhcp ip 1.1.1.2 timeout 120
config dhcp ip 1.1.1.2 port 8080
config dhcp ipv4 1.1.1.3
config interface id 12
config interface name abc id 12
config interface id 13
config interface name xyz id 13`;
// 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