Forum Moderators: phranque
var a = `
Test this one:
<a href="www.google.com/?id=1234&utm_foo=ONE&utm_bar=TWO&start=0">one</a>
and this one:
<a href="www.google.com/?id=4321&ocid=THREE&startview=20&gclid=FOUR&start=0">two</a>
`.trim();
// remove params utm_\w+, ocid, trkid, and gclid
// split to rows for readability here, I don't think you can actually do this in JS
var utm_match = /
// $1
(
<a[^>]+href=
// $2
(
" |
'
)
[^>]+
[?&]
)
(?:
(?:
// the list of params to be removed
utm_\w+? |
ocid |
trkid |
gclid
)
=[^\2&]+&?
)
// $3
(
[^>]*>
)/gi;
while (utm_match.test(a))
a = a.replace(utm_match, '$1$3'); const div = document.createElement('div')
div.innerHTML = a
const nodeArr = Array.from(div.querySelectorAll("a"))
nodeArr.forEach((href) => {
if (href.href.toString().indexOf("utm_") >= 0 ) {
const [url,params] = href.href.toString().split('?')
console.log(
[
url,
params.split('&').filter(param => param.indexOf("utm_") < 0).join('&')
].join("?")
)
} else {
console.log(href.href)
}
})