const regex = /#\s*master\s+([^#]+)#\s+osd/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('#\\s*master\\s+([^#]+)#\\s+osd', 'gm')
const str = `# version
# Betaflight / SPEEDYBEEF405V3 (SBF4) 4.3.2 Apr 7 2023 / 03:15:27 (f156481e9) MSP API: 1.44
# start the command batch
batch start
# master
set gyro_lpf1_static_hz = 0
set gyro_lpf2_static_hz = 300
set dyn_notch_q = 500
set dyn_notch_min_hz = 100
set dyn_notch_max_hz = 500
set gyro_lpf1_dyn_min_hz = 0
set gyro_lpf1_dyn_max_hz = 400
set mag_hardware = NONE
set rc_smoothing_auto_factor = 35
# osd
set osd_vbat_pos = 471
set osd_rssi_pos = 449`;
// 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