Skip to content

Instantly share code, notes, and snippets.

@chhib
Created November 20, 2018 13:25
Show Gist options
  • Save chhib/4ba7238d595828fd6b7dca73da8c72ac to your computer and use it in GitHub Desktop.
Save chhib/4ba7238d595828fd6b7dca73da8c72ac to your computer and use it in GitHub Desktop.
Code from the Query String Parameters video on DevTips
const URL = 'https://www.adlibris.com' +
'/se/bok/factfulness-tio-knep-som-hjalper-dig-forsta-varlden-9789127149946' +
'?filter=hardcover' +
'&gclid=760a19f0-5057-417a-8e6a-b44a01b12a46' +
'&some=thing'
const [first, last] = URL.split("?")
const URLwithoutGCLID = first
URLwithoutGCLID
const URL = 'https://www.adlibris.com' +
'/se/bok/factfulness-tio-knep-som-hjalper-dig-forsta-varlden-9789127149946' +
'?hej=monika' +
'&filter=hardcover' +
'&some=thing' +
'&gclid=760a19f0-5057-417a-8e6a-b44a01b12a46'
const a = window.document.createElement('a')
a.href = URL
let params = new URLSearchParams(a.search)
params.delete('gclid')
const first = URL.split("?")[0]
const newParams = params.toString()
const URLwithoutGCLID = `${first}?${newParams}`
URLwithoutGCLID
const URL = 'https://www.adlibris.com' +
'/se/bok/factfulness-tio-knep-som-hjalper-dig-forsta-varlden-9789127149946' +
'?gclid=760a19f0-5057-417a-8e6a-b44a01b12a46' +
'&filter=hardcover' +
'&some=thing'
/*
Using Kip's answer at:
https://stackoverflow.com/questions/1842681/regular-expression-to-remove-one-parameter-from-query-string
*/
const reFilterGCLID = /&gclid(\=[^&]*)?(?=&|$)|^gclid(\=[^&]*)?(&|$)/
const [first, last] = URL.split("?")
const paramsWithoutGCLID = last.replace(reFilterGCLID, '')
const URLwithoutGCLID = `${first}?${paramsWithoutGCLID}`
URLwithoutGCLID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment