Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Compactify the query string

Introduction

Just on Hacker news, John Resig contemplates transforming a query "foo=1&foo=2&foo=3&blah=a&blah=b" into one that looks like this: "foo=1,2,3&blah=a,b", https://johnresig.com/blog/search-and-dont-replace/. He claims "being 10 lines shorter than Mark’s solution".

Query strings consist of sequences of name-value pairs. Name-value pairs consist of a name and value, separated by =. names and values are possibly empty sequences of alphanumeric characters. Name-value pairs are separated by the & character in the sequence. Values are unique for each name.

Challenge

10 lines shorter than Mark's solution is not enough.

  • Read a query string.
  • Combine name value pairs with equal name into a single name value pair, with values concatenated and separated by comma.
  • Output the combined query string, with name-value pairs in the order the names are found from left to right in the query string.

This is code golf, the standard loopholes are closed.

Example Input and Output

Input:

foo=1&foo=&blah=a&foo=3&bar=x&blah=b&=1&=2

Output:

foo=1,,3&blah=a,b&bar=x&=1,2

Input:

foo=bar&foo=foo

Output:

foo=bar,foo

Answer*

Draft saved
Draft discarded
Cancel
2
  • \$\begingroup\$ You can replace All with 2 to save 2 bytes. \$\endgroup\$ Commented Sep 30, 2020 at 20:40
  • \$\begingroup\$ @LegionMammal978 You should post it. I didn't even know about StringExtract. \$\endgroup\$ Commented Oct 1, 2020 at 2:28

AltStyle によって変換されたページ (->オリジナル) /