It's important to URL encode query parameters before the request is sent. This helps to avoid issues with characters that have special meaning (?
, =
, &
, #
, etc.)
So instead of sending literal ampersand characters &
in the regex, it should be URL-encoded to be %26
instead.
/austin/query.html?dept=([^%26]*)&group=([^%26]*)
When this parsed by the querystring
module, it will automatically be converted back to the ampersand character.
const querystring = require('querystring');
const URL = require('url');
function parseQueryParamsFromUrlPath(urlPath) {
const { query } = URL.parse(urlPath);
return querystring.parse(query);
}
parseQueryParamsFromUrlPath('/austin/query.html?dept=([^%26]*)&group=([^%26]*)');
// Output: { dept: '([^&]*)', group: '([^&]*)' }