$re = '/(\(\s?function+.formula.*\{\s?)([\w|\.]+\s?=[^\}]*)(\s?\}\s?\)\s?\(\s?\)\s?;)/i';
$str = ' (function Formula(x){ ListaOP.FormaPActiva.RemanenteACancelar = ListaOP.OPActiva.TotalesCliente.Total - ListaOP.OPActiva.MontoAcumuladoFormasPago; })(variableX);
(function Formula() { ListaOP.FormaPActiva.ImporteACancelar = ListaOP.FormaPActiva.RemanenteACancelar; })();
(function formula(){ a=b; })();
if (!v.Parametros.AfectarGastoEnvioRecargoFinanciero && ListaOP.OPActiva.MontoCanceladoContado < v.GastoEnvio + v.GastoEnvioImpuestoCliente) {
(function formula() { ListaOP.FormaPActiva.RemanenteACancelar = ListaOP.FormaPActiva.RemanenteACancelar - v.GastoEnvio - v.GastoEnvioImpuestoCliente; })();
}
var c = function(){
};
(function Formula() { ListaOP.FormaPActiva.ImporteFinanciamientoTotal = ListaOP.FormaPActiva.RemanenteACancelar * (ListaOP.FormaPActiva.CoeficienteFinanciacion - 1); })();
(function Formula() { ListaOP.FormaPActiva.ImporteACancelar = ListaOP.FormaPActiva.ImporteACancelar + ListaOP.FormaPActiva.ImporteFinanciamientoTotal; })();
(function Formula(x) { ListaOP.FormaPActiva.RemanenteACancelar = ListaOP.FormaPActiva.ImporteACancelar; })(10);';
$subst = "Calcular('$2'); ";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$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 PHP, please visit: http://php.net/manual/en/ref.pcre.php