const regex = /(?:\A|;)(?:\s*)((?:%macro |%inc(?:lude)?\b|data |proc )[^;]+)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:\\A|;)(?:\\s*)((?:%macro |%inc(?:lude)?\\b|data |proc )[^;]+)', 'g')
const str = `%macro foo;
%inc "foo.sas";
%mend;
%macro mylogit1(all_deps, outest);
%let k=1;
%let dep = %scan(&all_deps, &k);
%do %while("&dep" NE "");
title "dependent variable is &dep";
proc logistic data=xxx des outest=_est&k;
model &dep = ind1 ind2;
run;
%let k = %eval(&k + 1);
%let dep = %scan(&all_deps, &k);
%end;
%if "&outest" NE "" %then
%do;
data &outest;
set
%do i = 1 %to &k - 1;
_est&i
%end;
;
run;
%let k = %eval(&k - 1);
proc datasets;
delete _est1 - _est&k;
run;
%end;
%else
%do;
%put no dataset name was provided, files are not combined;
%end;
%mend;
%mylogit1(v1 v2 v3);
%mylogit1(v1 v2 v3, a);
proc print data = a;
var intercept ind1 ind2;
run;
`;
const subst = ``;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
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