adriaan
adriaan

Reputation: 1118

Obfuscate all query params except whitelisted

I'm setting up a tracking script which tries to get not too much personal data. I don't want to store the value of all the params except a few.

Let's assume we have this variable coming from window.location.search:

var search = '?utm_source=telegram&rel=twitter&password=nooooooo&utm_medium=phone';

I did try a few hours with regexes but I can't make it work:

search.replace(/([?&][^=|^(utm_)]+=)[^&#]*/gi, '$1***')

// ?utm_source=telegram&rel=***&password=***&utm_medium=phone

But I would love to have this output:

?utm_source=telegram&rel=twitter&password=***&utm_medium=phone

So it should replace the values of all the parameters with *** except for the parameters starting with utm_ or being rel.

Upvotes: 0

Views: 341

Answers (1)

Julio
Julio

Reputation: 5308

You may try with this:

\b((?!rel|utm_)\w+)=[^&]+(?=&|$)

Replace by: $1=***

Demo

Explained:

\b                  # Word boundary
((?!rel|utm_)\w+)   # A word that does not start with rel or utm_
=                   # literal =
[^&]+               # Any non & character repeated 1 or more.
                    # That will match the value
(?=&|$)             # Followed by & or end of line/string

Upvotes: 1

Related Questions