混淆除白名单外的所有查询参数

时间:2018-08-14 14:48:35

标签: javascript regex

我正在设置一个跟踪脚本,该脚本试图获取不太多的个人数据。我不想存储除少数几个参数之外的所有参数的值。

假设我们有这个变量来自together.o

main

我确实使用了正则表达式尝试了几个小时,但我无法使其正常工作:

window.location.search

但是我很想得到这个输出:

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

因此,应将所有参数的值替换为search.replace(/([?&][^=|^(utm_)]+=)[^&#]*/gi, '$1***') // ?utm_source=telegram&rel=***&password=***&utm_medium=phone ,但以?utm_source=telegram&rel=twitter&password=***&utm_medium=phone 开头或为***的参数除外。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

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

替换为:$1=***

Demo

说明:

\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
相关问题