最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatting HTTP headers using regular expressions - Stack Overflow

programmeradmin1浏览0评论

I want to format my HTTP headers using regex. I've done it using split(' ') followed by array manipulation, however this time I want to perform this operation using regex.

I want to take this input which is a giant string:

GET /v1/search?q=bob%20dylan&type=artist HTTP/1.1
Host: api.spotify
Cache-Control: no-cache
Postman-Token: e2f09f98-f8e0-43f7-5f0e-b16e670399e2

and format it to be an object as so:

{ headers: 
   { Host: ' api.spotify',
     'Cache-Control': ' no-cache',
     'Postman-Token': ' e2f09f98-f8e0-43f7-5f0e-b16e670399e2' 
   },
  verb: 'GET',
  path: '/v1/search?q=bob%20dylan&type=artist',
  protocol: 'HTTP/1.1' 
}

I understand by using the split method, my code is more readable. However, my first attempt was to use regex since my goal was to extract/format a string.

I know it is possible through regex, but is it even worth it? What does everyone think?

Thank you for your time.

I want to format my HTTP headers using regex. I've done it using split(' ') followed by array manipulation, however this time I want to perform this operation using regex.

I want to take this input which is a giant string:

GET /v1/search?q=bob%20dylan&type=artist HTTP/1.1
Host: api.spotify.
Cache-Control: no-cache
Postman-Token: e2f09f98-f8e0-43f7-5f0e-b16e670399e2

and format it to be an object as so:

{ headers: 
   { Host: ' api.spotify.',
     'Cache-Control': ' no-cache',
     'Postman-Token': ' e2f09f98-f8e0-43f7-5f0e-b16e670399e2' 
   },
  verb: 'GET',
  path: '/v1/search?q=bob%20dylan&type=artist',
  protocol: 'HTTP/1.1' 
}

I understand by using the split method, my code is more readable. However, my first attempt was to use regex since my goal was to extract/format a string.

I know it is possible through regex, but is it even worth it? What does everyone think?

Thank you for your time.

Share Improve this question asked Feb 22, 2017 at 20:07 kdizzlekdizzle 6271 gold badge12 silver badges24 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 5

This should work for you:

const data = `GET /v1/search?q=bob%20dylan&type=artist HTTP/1.1
Host: api.spotify.
Cache-Control: no-cache
Postman-Token: e2f09f98-f8e0-43f7-5f0e-b16e670399e2`

const format = data => {
    const headers = {}
    const result = { headers }
    const regex = /([\w-]+): (.*)/g
    let temp
    while (temp = regex.exec(data)) {
        headers[temp[1]] = temp[2]
    }
    temp = data.match(/(\w+)\s+(.*?)\s+(.*)/)
    result.verb = temp[1]
    result.path = temp[2]
    result.protocol = temp[3]
    return result
}

console.log(format(data))

/([\w-]+): (.*)/g this regex will match any header-name: value and capture it like so ['header-name: value', 'header-name', 'value']

then we asign it to headers object where header-name is key and value is value

at the end we parse first line to get rest of information

How it works

(\w+) match and capture 1 or more word characters
\s+ match 1 or more whitespace (.*?)match and capture any char not gready *?
\s+ until one or more white space is found
(.*) match evrything (until end of line)

You can use .split() with RegExp \s/ where the first three elements of array returned by .split() should be verb, path, protocol; utilize .shift() on first three elements, with the remainder of results set as property, value pairs at headers object using current index and next index of array, until array .length evaluates to false at condition of while loop.

let getHeaders = headers => {

  let h = headers.split(/\s/);

  let o = {
    verb: h.shift(),
    path: h.shift(),
    protocol: h.shift(),
    headers: {}
  };

  while (h.length) {
    o.headers[h.shift()] = h.shift();
  }
  
  return o
};

var str = `GET /v1/search?q=bob%20dylan&type=artist HTTP/1.1
Host: api.spotify.
Cache-Control: no-cache
Postman-Token: e2f09f98-f8e0-43f7-5f0e-b16e670399e2`;

console.log(getHeaders(str));

This Should Work.

Search by:

(GET)\s(.+)\s(HTTP\/\d+\.\d+)\n(Host):\s(.+)$\n(Cache-Control):\s(.+)$\n(Postman-Token):\s(.+)$

Replace with:

{ 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}

JavaScript Code:

const regex = /(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.
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(result);

Input:

GET /v1/search?q=bob%20dylan&type=artist HTTP/1.1
Host: api.spotify.
Cache-Control: no-cache
Postman-Token: e2f09f98-f

Output:

{ headers:    
    { Host 'api.spotify.',
      'Cache-Control': 'no-cache',
      'Postman-Token': 'e2f09f98-f'
    }, 
    verb: 'GET',
    path: '/v1/search?q=bob%20dylan&type=artist',
    protocol: 'HTTP/1.1'
}

See: https://regex101./r/3DKEas/4

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>