const regex = /(GET)\s(.+)\s(HTTP\/\d+\.\d+)\n(Host):\s(.+)$\n(Cache-Control):\s(.+)$\n(Postman-Token):\s(.+)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(GET)\\s(.+)\\s(HTTP\\\/\\d+\\.\\d+)\\n(Host):\\s(.+)$\\n(Cache-Control):\\s(.+)$\\n(Postman-Token):\\s(.+)$', 'gm')
const str = `GET /v1/search?q=bob%20dylan&type=artist HTTP/1.1
Host: api.spotify.com
Cache-Control: no-cache
Postman-Token: e2f09f98-f`;
const subst = `{ headers: \n\t{ $4 '$5',\n\t '$6': '$7',\n\t '$8': '$9'\n\t}, \n\tverb: '$1',\n\tpath: '$2',\n\tprotocol: '$3'\n}`;
// 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