i have a scenario where i will get input as path and query param and in the place of value i will get a regex.
regex input contains & which is a delimiter in query param.
`Input :` '/austin/query.html?dept=([^&]*)&group=([^&]*)'
i want to get this regex ([^&]*)
from query param dynamically.
Any idea or suggestions might be silly / basic question please do help ?
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: '([^&]*)' }