const regex = /(\[.*\])(=>?)\s+(\w+[\(\)\d+\.]+)\s+?(\".*\"|\{[\s\w]+\})?/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(\\[.*\\])(=>?)\\s+(\\w+[\\(\\)\\d+\\.]+)\\s+?(\\".*\\"|\\{[\\s\\w]+\\})?', 'g')
const str = `array(40) {
["HTTP_AUTHORIZATION"]=>
string(0) ""
["HTTP_HOST"]=>
string(12) "galtrade.loc"
["HTTP_CONNECTION"]=>
string(10) "keep-alive"
["HTTP_PRAGMA"]=>
string(8) "no-cache"
["HTTP_CACHE_CONTROL"]=>
string(8) "no-cache"
["HTTP_UPGRADE_INSECURE_REQUESTS"]=>
string(1) "1"
["HTTP_USER_AGENT"]=>
string(142) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 YaBrowser/19.3.0.2485 Yowser/2.5 Safari/537.36"
["HTTP_ACCEPT"]=>
string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
["HTTP_ACCEPT_ENCODING"]=>
string(13) "gzip, deflate"
["HTTP_ACCEPT_LANGUAGE"]=>
string(11) "ru,en;q=0.9"
["HTTP_COOKIE"]=>
string(59) "fdafa396c9597e51e294ef0cc64573a0=c0ntndm6dh8bg3vk901nrs1a05"
["PATH"]=>
string(502) "c:\\ospanel\\modules\\php\\PHP-5.6\\ext;c:\\ospanel\\modules\\php\\PHP-5.6\\pear;c:\\ospanel\\modules\\php\\PHP-5.6\\pear\\bin;c:\\ospanel\\modules\\php\\PHP-5.6;c:\\ospanel\\modules\\imagemagick;c:\\ospanel\\modules\\wget\\bin;c:\\ospanel\\modules\\git;c:\\ospanel\\modules\\git\\bin;c:\\ospanel\\modules\\git\\mingw\\bin;c:\\ospanel\\modules\\git\\cmd;c:\\ospanel\\modules\\database\\MySQL-5.6\\bin;c:\\ospanel\\modules\\http\\Apache-2.4\\bin;c:\\ospanel\\modules\\http\\Apache-2.4;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\system32\\Wbem;C:\\Windows\\SysWOW64"
["SystemRoot"]=>
string(10) "C:\\Windows"
["COMSPEC"]=>
string(27) "C:\\Windows\\system32\\cmd.exe"
["PATHEXT"]=>
string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
["WINDIR"]=>
string(10) "C:\\Windows"
["SERVER_SIGNATURE"]=>
string(0) ""
["SERVER_SOFTWARE"]=>
string(6) "Apache"
["SERVER_NAME"]=
string(12) "galtrade.loc"
["SERVER_ADDR"]=>
string(9) "127.0.0.1"
["SERVER_PORT"]=>
string(2) "80"
["REMOTE_ADDR"]=>
string(9) "127.0.0.1"
["DOCUMENT_ROOT"]=>
string(23) "H:/Domains/galtrade.loc"
["REQUEST_SCHEME"]=>
string(4) "http"
["CONTEXT_PREFIX"]=>
string(0) ""
["CONTEXT_DOCUMENT_ROOT"]=>
string(23) "H:/Domains/galtrade.loc"
["SERVER_ADMIN"]=>
string(18) "[no address given]"
["SCRIPT_FILENAME"]=>
string(33) "H:/Domains/galtrade.loc/index.php"
["REMOTE_PORT"]=>
string(5) "45846"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["REQUEST_METHOD"]=>
string(3) "GET"
["QUERY_STRING"]=>
string(0) ""
["REQUEST_URI"]=>
string(1) "/"
["SCRIPT_NAME"]=>
string(10) "/index.php"
["PHP_SELF"]=>
string(10) "/index.php"
["REQUEST_TIME_FLOAT"]=>
float(1552820558.246)
["REQUEST_TIME"]=>
int(1552820558)
["argv"]=>
array(0) {sddsdsd
}
["argc"]=
int(0)
}`;
const subst = `<i>$1</i><i>$2</i><i>$3</i><i>$4</i>`;
// 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