const regex = /\b(.*?])\s(__declspec.+?\)\))(.*?;)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\b(.*?])\\s(__declspec.+?\\)\\))(.*?;)', 'gm')
const str = `#if defined (_MSC_VER) && !defined(__clang__)
double pU0[3*4*K_MAX_STACK] __declspec(align(64));
#else
double pU0[3*4*K_MAX_STACK] __attribute__ ((aligned (64)));
#endif
#if defined (_MSC_VER) && !defined(__clang__)
double CC[16] __declspec(align(64)) = {0};
#else
double CC[16] __attribute__ ((aligned (64))) = {0};
#endif
#if defined (_MSC_VER) && !defined(__clang__)
double pU[3*4*K_MAX_STACK] __declspec(align(64));
#else
double pU[3*4*K_MAX_STACK] __attribute__ ((aligned (64)));
#endif`;
const subst = `$2 $1$3`;
// 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