JavaScript Formatting HTTP headers using regular expressions sample http headers,sample http header fields,http headers formatting

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.com
Cache-Control: no-cache
Postman-Token: e2f09f98-f8e0-43f7-5f0e-b16e670399e2

and format it to be an object as so:

{ headers: 
   { Host: ' api.spotify.com',
     '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.

Answer:1

This should work for you:

const data = `GET /v1/search?q=bob%20dylan&type=artist HTTP/1.1
Host: api.spotify.com
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))
Answer:2

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.com
Cache-Control: no-cache
Postman-Token: e2f09f98-f8e0-43f7-5f0e-b16e670399e2`;

console.log(getHeaders(str));
Answer:3

I have : <script> var s="/xyz/abc/5-6-12-0-0-1-0"; document.write(s.match(/((\d+)$)/g)); </script> I've tried everything I know but I can not figure out how to get reasonable results "1"...

I have : <script> var s="/xyz/abc/5-6-12-0-0-1-0"; document.write(s.match(/((\d+)$)/g)); </script> I've tried everything I know but I can not figure out how to get reasonable results "1"...

  1. retrieve value javascript
  2. retrieve input value javascript
  3. retrieve form value javascript
  4. retrieve textarea value javascript
  5. retrieve select value javascript
  6. retrieve button value javascript
  7. retrieve css value javascript
  8. retrieve option value javascript
  9. retrieve value from javascript function
  10. retrieve value from javascript object
  11. retrieve label value javascript
  12. retrieve checkbox value javascript
  13. retrieve key value javascript
  14. retrieve value from javascript function in code behind
  15. retrieve number value javascript
  16. retrieve return value javascript
  17. retrieve value type javascript
  18. retrieve value from textbox javascript
  19. retrieve value from localstorage javascript
  20. retrieve value from dropdown javascript

I found myself wanting to return a 180 degree hue-rotated color from a function. My project's using D3, so that's the library I'm using to do this. Unfortunately, D3 doesn't come with a function to ...

I found myself wanting to return a 180 degree hue-rotated color from a function. My project's using D3, so that's the library I'm using to do this. Unfortunately, D3 doesn't come with a function to ...

I'm looking to append elements conditionally to a container based on whether the tags data matches the key variable. The way tags' data is returned is like tag1 tag2 tag3 tag4 tag5, the key is ...

I'm looking to append elements conditionally to a container based on whether the tags data matches the key variable. The way tags' data is returned is like tag1 tag2 tag3 tag4 tag5, the key is ...

I'd like to track a form on my real-estate website. The difficulty is that I have 3 radio buttons (only one can be chosen) and I don't know how to link the button chosen to the event that will be send ...

I'd like to track a form on my real-estate website. The difficulty is that I have 3 radio buttons (only one can be chosen) and I don't know how to link the button chosen to the event that will be send ...

  1. google analytics event tracking
  2. google analytics event category
  3. google analytics event value
  4. google analytics event tracking examples
  5. google analytics event goals
  6. google analytics event funnel
  7. google analytics event flow
  8. google analytics event types
  9. google analytics event tag
  10. google analytics event timestamp
  11. google analytics event category examples
  12. google analytics event limits
  13. google analytics event setup
  14. google analytics event value string
  15. google analytics event custom dimension
  16. google analytics event label not set
  17. google analytics event count
  18. google analytics event label character limit
  19. google analytics event code generator
  20. google analytics event tracking gtag