const regex = /GigabitEthernet1\/1\/1(?:(?!interface GigabitEthernet)[\s\S])*(vlan.*40(?:9[12]|[6-8][0-9]|5[2-9]))/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('GigabitEthernet1\\\/1\\\/1(?:(?!interface GigabitEthernet)[\\s\\S])*(vlan.*40(?:9[12]|[6-8][0-9]|5[2-9]))', 'gm')
const str = `interface GigabitEthernet1/1/1
description link-to-someswitch-Gi2/0/1
switchport access vlan 3109
switchport trunk allowed vlan 300,301,350,358,800,3109
switchport trunk encapsulation dot1q
switchport trunk native vlan 3109
switchport mode dynamic desirable
srr-queue bandwidth share 40 20 20 20
srr-queue bandwidth shape 10 0 0 0
priority-queue out
no snmp trap link-status
mls qos trust dscp
spanning-tree portfast disable
!
interface GigabitEthernet1/1/2
description link-to-someswitch2-Gi2/0/1
switchport access vlan 3609
switchport trunk allowed vlan 300,301,350,358,800,3609,4088
switchport trunk encapsulation dot1q
switchport trunk native vlan 3109
switchport mode dynamic desirable
srr-queue bandwidth share 40 20 20 20
srr-queue bandwidth shape 10 0 0 0
priority-queue out
no snmp trap link-status
mls qos trust dscp
spanning-tree portfast disable
interface GigabitEthernet1/1/1
description link-to-someswitch-Gi2/0/1
switchport access vlan 3109
switchport trunk allowed vlan 300,301,350,358,800,4088
switchport trunk encapsulation dot1q
switchport trunk native vlan 3109
switchport mode dynamic desirable
srr-queue bandwidth share 40 20 20 20
srr-queue bandwidth shape 10 0 0 0
priority-queue out
no snmp trap link-status
mls qos trust dscp
spanning-tree portfast disable
`;
// 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