Skip to content

Instantly share code, notes, and snippets.

@paulmwatson
Last active June 7, 2021 12:37
Show Gist options
  • Save paulmwatson/49b9f25aa75726764bcacc6febdd6247 to your computer and use it in GitHub Desktop.
Save paulmwatson/49b9f25aa75726764bcacc6febdd6247 to your computer and use it in GitHub Desktop.
Manipulating the URL via strings
let url = 'https://host.test/path/here.filetype?query=string&more=params#anchor';
const querystringStart = url.indexOf('?');
const anchorStart = url.indexOf('#');
const querystring = url.slice(querystringStart + 1, anchorStart);
let querystringParams = querystring.split('&');
let newQuerystringParams = [];
querystringParams.forEach((queryparam, i) => {
let param = queryparam.split('=');
if (param[0] !== 'more') {
newQuerystringParams.push(`${param[0]}=${param[1]}-changed`)
}
});
newQuerystringParams.push('new=param')
const newQuerystring = newQuerystringParams.join('&');
const newUrl = `${url.slice(0, querystringStart)}?${newQuerystring}${url.slice(anchorStart)}`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment