Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const paragraph = 'foo=1&foo=2&foo=3&blah=a&blah=b';
- const regex = /([^=&]+)=([^&]*)/g
- const array = [...paragraph.matchAll(regex)]
- const obj = array.reduce((q,[kv, key, value]) => {
- q[key] = (q[key] ? q[key] + `,`: ``) + value
- return q
- }, {});
- const result = Object.keys(obj).map(a => `${a}=${obj[a]}`).join(`&`)
- console.log(result)
- // expected output: "foo=1,2,3&blah=a,b"
Advertisement
Add Comment
Please, Sign In to add comment