URL Query Encoding: Why C++ Loses Its Plus Signs in Search Links
Follow a workshop title from a search box into a shareable URL and back. Find out when + means a space, when it means plus, and which code should do the encoding.

A workshop catalog has classes on both C and C++. Type C++ into the search box and the right class appears. Choose “Copy search link,” open the link in another tab, and the results are suddenly about C. The index is fine. So is the clipboard. The search box in the second tab received three characters: a C and two spaces. The catalog trimmed the spaces before looking up the class, hiding the most useful clue.
You can reproduce the missing plus signs without a website, a server, or even a network connection. The key is to distinguish the text somebody typed from the instructions for carrying that text in a URL. A plus sign has no universal meaning of “space” throughout a URL. One particular way of reading the query gives it that meaning.
Catch the bug before search gets involved
Run these lines in a browser console. The leading question mark that normally introduces a URL query is optional for this constructor. The quotes in the last result come from JSON.stringify; they let you see the spaces that an ordinary console display might conceal.
const broken = new URLSearchParams('q=C++');
JSON.stringify(broken.get('q')); // '"C "'
URLSearchParams treats a string argument as already serialized form data. When it reads a field, a raw + stands for a space. A percent escape, on the other hand, stands for a byte. %2B represents the byte for the plus character. The reader replaces raw plus signs before interpreting the percent escapes. That order matters: a plus recovered from %2B stays a plus; it does not go back through the space rule.
Compare the two queries below, keeping the reader the same. Both result strings have length three. A length check would pass even when the catalog opened the wrong class. Test the characters or compare the whole string to the title you started with.
new URLSearchParams('q=C++').get('q') === 'C '; // true
new URLSearchParams('q=C%2B%2B').get('q') === 'C++'; // true
Give the writer a value, not a fragment of query text
The catalog also has a workshop called C++ & Rust. Its share link must remember both the search term and the selected day. If you build that link by placing the title after ?q= and appending &day=sat, the reader cannot tell the ampersand in the title from the one introducing the day. It splits the title in two before the search code ever sees it. Changing the way the search box displays spaces cannot put those pieces back together.
Instead, start with a URL and give its search parameters a name and a value. Using new URL here just constructs an object. It does not fetch the address. The example domain is a convenient blank canvas, not a catalog you need to visit.
const link = new URL('https://example.org/classes');
link.searchParams.set('q', 'C++ & Rust');
link.searchParams.set('day', 'sat');
link.search; // '?q=C%2B%2B+%26+Rust&day=sat'
new URL(link.href).searchParams.get('q'); // 'C++ & Rust'
new URL(link.href).searchParams.get('day'); // 'sat'
There are two different jobs for the ampersand in this result. %26 carries an ampersand inside the value; the unescaped & between Rust and day separates fields. The same distinction explains the plus signs. The two %2B sequences carry literal punctuation, while the two raw plus signs carry the spaces on either side of the title's ampersand. There's no ambiguity left for the reader to resolve.
Why split the fields before decoding their contents? Take the correctly serialized link and decode the whole query first. The escaped ampersand in the title becomes indistinguishable from the separator before the day. Splitting that result on ampersands produces three pieces instead of the intended two. A parameter parser takes the opposite route: it identifies the fields while the punctuation still says which pieces belong together, then reads each name and value. The URL Standard describes that order. It works without a special case for workshop names or a growing list of characters to replace.
const query = 'q=C%2B%2B+%26+Rust&day=sat';
Array.from(new URLSearchParams(decodeURIComponent(query)).entries());
// [['q', 'C '], [' Rust', ''], ['day', 'sat']]
Inspect all the entries, not just q. The stray field named Rust has an empty string for its value: a useful sign that the title was split at its ampersand.
Notice that the check reads a fresh URL built from link.href. Looking at the input variable would confirm only what the writer meant to send. Reading the finished address exercises the same handoff that failed when somebody opened the shared link. It is a useful place for a regression test: it needs neither the search index nor a particular ordering of results.
Why the component encoder writes spaces as %20
Open URL Encode / Decode. With Mode: Encode and Scope: Component, paste C++ & Rust into the input and choose Run. The output is C%2B%2B%20%26%20Rust. Switch the mode to Decode: the tool moves its previous output into the input. Choose Run again and the workshop title comes back intact. You have encoded and decoded a single component, not parsed a list of form fields.
That middle spelling differs from the query we just built. The component encoder writes %20 for a space. The form serializer writes + for a space. The form reader understands both spellings, so either of these query strings retrieves the same title. The reverse is not generally true: a plain component decoder interprets percent escapes but does not turn raw plus signs into spaces.
const component = 'C%2B%2B%20%26%20Rust';
const formValue = 'C%2B%2B+%26+Rust';
new URLSearchParams('q=' + component).get('q'); // 'C++ & Rust'
new URLSearchParams('q=' + formValue).get('q'); // 'C++ & Rust'
decodeURIComponent(formValue); // 'C+++&+Rust'
For another control, select Encode and Full URL in the tool, replace the input with the original title, and run it. The result is C++%20&%20Rust. This mode leaves reserved punctuation such as & available to do structural work in an address. It has no way to know that the particular ampersand you supplied belongs to a workshop title. Use Component to inspect a value in isolation. To construct the catalog link, let the parameter writer take the raw title and supply the boundaries itself.
The same percent sign can be data next time
A tempting repair is to run the title through the component encoder first, then pass the output to searchParams.set. Try it with just C++. The first encoder produces C%2B%2B. Now the parameter writer receives a C, two percent signs, two digits, and two letters. It was promised ordinary text, so it faithfully preserves every character. To carry a literal percent sign it writes %25. It does not inspect the next two characters and guess whether you secretly meant something else.
const twice = new URLSearchParams();
twice.set('q', encodeURIComponent('C++'));
twice.toString(); // 'q=C%252B%252B'
new URLSearchParams(twice.toString()).get('q'); // 'C%2B%2B'
Why not decode the result once more at the far end? Imagine the catalog adds a lesson on percent encoding and its actual search term is %2B. With the extra decode, a search for those three printed characters becomes a search for +. Now one class works at the expense of another. A visible %25 is worth investigating, but on its own it is not proof of a bug. First find out which text the user meant to transport.
There is a similar trap in trying to recognize already encoded inputs by their appearance. The strings you want to distinguish can be literally identical: one person pasted the completed component for a plus, another typed the name of the escape they want to study. No regular expression can recover that history. Give a function an explicit contract instead. A link builder takes a raw title; a reader of an existing link takes a serialized address. Do not route both kinds of input through a helper named simply “clean.”
Edit a bookmarked search without dropping its filters
A returning visitor has bookmarked both days of the workshop and scrolled down to its schedule. Their link has two fields named day, a fragment named schedule, and an older spelling of the search term that uses %20 for a space. When they reopen the filter panel and press Save without changing anything, should the address have to match the bookmark character for character? Try this small experiment before deciding what your test should assert.
const saved = new URL('https://example.org/classes?q=C%2B%2B%20basics&day=sat&day=sun#schedule');
const before = saved.href;
saved.searchParams.set('q', saved.searchParams.get('q'));
saved.href === before; // false
saved.searchParams.get('q'); // 'C++ basics'
saved.searchParams.getAll('day'); // ['sat', 'sun']
saved.hash; // '#schedule'
Assigning the current search term back to itself asks the parameter writer to serialize the fields again. The old %20 becomes +. The workshop title, both days, and the scroll destination survive. Comparing the two address strings would flag a difference, but it would not tell you whether the share feature still works. For this catalog, compare the decoded title, the list of selected days, and the fragment. If you instead promise to preserve a pasted address verbatim, keep that original string; rebuilding it is the wrong operation for that promise.
Use getAll for the days because get reports only the first occurrence. A test that checks only Saturday cannot catch Sunday disappearing. The same distinction applies when writing filters: set replaces the values under that name, while append adds another occurrence. Use set for the single search box. For a control that accepts several days, write each chosen day as its own entry. Neither choice requires touching the plus signs in the title.
Keep the fix inside the query
Suppose the same site has a path named /classes/C++. Inspect new URL('https://example.org/classes/C++').pathname: the two plus signs remain. The form rule belongs to query field parsing, not to ordinary path text. Replacing every plus sign with a space in the whole address would change a valid path to fix a bug somewhere else. Likewise, when you already have an absolute address, use new URL(address).searchParams, not new URLSearchParams(address). The latter expects query text, not a complete address to take apart.
When tracking a report, keep three samples side by side: the original search box value, the serialized query copied by the share button, and the value the receiving tab reads before trimming. Compare the first with the third. If they agree, changing the URL encoder will not fix a search index that handles punctuation incorrectly. If they differ, inspect the middle sample to see where the raw plus, field separator, or extra percent escape entered the picture. This gives each person maintaining the catalog a small, reproducible test instead of a screenshot of disappointing results.
Keep a space and a literal plus as separate cases in that test. Add the two together, then include an ampersand and a literal percent escape. Ordinary words alone cannot distinguish a correct link builder from string concatenation. The goal is not to make the URL look like the title. It is to make the reader receive exactly the title that somebody typed.