const regex = /Default Output Device:((?!Output Source: )[\s\S])*Output Source: (.*)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('Default Output Device:((?!Output Source: )[\\s\\S])*Output Source: (.*)$', 'gm')
const str = `Audio:
Devices:
Built-in Microphone:
Default Input Device: Yes
Input Channels: 2
Manufacturer: Apple Inc.
Current SampleRate: 44100
Transport: Built-in
Input Source: Internal Microphone
Built-in Output:
Default Output Device: Yes
Default System Output Device: Yes
Manufacturer: Apple Inc.
Output Channels: 2
Current SampleRate: 44100
Transport: Built-in
Output Source: Internal Speakers
DisplayPort:
Manufacturer: Apple Inc.
Output Channels: 2
Current SampleRate: 48000
Transport: DisplayPort
Output Source: DELL U2715H
ZoomAudioDevice:
Input Channels: 2
Manufacturer: zoom.us
Output Channels: 2
Current SampleRate: 48000
Transport: Virtual
Input Source: Default
Output Source: Default
`;
// 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