It is best not to use bare \n or \r in regex patterns. Instead, use \s* or \s+ with multi-line flags for better compatibility.
const match = new RegExp(
String.raw`#\s*{$name}\s+([\s\S]+?)\s*#\s+osd`,
"gm"
).exec(code);
For detailed explanation, see here
#\s*master\s+([^#]+)#\s+osd may be also possible; see here.