const regex = new RegExp('(?i)\\(\\s*((?:const\\s+)?(?:u?(?:byte|int)\\d*|void)(?:\\s*\\*)?)\\s*\\)\\s*(\\w+(?:\\((?>[^()]+|(?<c>)\\(|(?<-c>)\\))*\\))?)', 'gm')
const str = `#Pass 1
(void)memcpy(&a[0],(void * )hihi, (UBYTE) V_SIZE);
(void) memcpy((VOID*)abc, (const VOID*) dafa, (uint8)NUMBER_SIZE);
(void )memcpy(
(void *)p,
&abc, (uint8)DATE_SIZE);
#Pass 2
static_cast<void>(memcpy(&a[0],(void * )hihi, (UBYTE) V_SIZE));
static_cast<void>(memcpy((VOID*)abc, (const VOID*) dafa, (uint8)NUMBER_SIZE));
static_cast<void>(memcpy(
(void *)p,
&abc, (uint8)DATE_SIZE));`;
const subst = `static_cast<$1>($2)`;
// 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