javascript - 混淆除白名单之外的所有查询参数
问题描述
我正在设置一个跟踪脚本,试图获取不太多的个人数据。我不想存储除少数之外的所有参数的值。
假设我们有这个变量来自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
所以它应该替换所有参数的值,***
除了以utm_
或开头的参数rel
。
解决方案
你可以试试这个:
\b((?!rel|utm_)\w+)=[^&]+(?=&|$)
替换为:$1=***
解释:
\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
推荐阅读
- python - 我在 pygame 中创建了一个没有动画的基本平台游戏。我已经为左右移动使用了事件处理,但我无法添加跳跃机制
- javascript - 在反应中导出 PDF?
- c - graphics.h 库的一些功能在 linux 的 c 文件中不起作用(ubuntu LTS 20.04)
- python - 如何使用字典将这些整数元组列表翻译成名称元组列表?
- swift - 导出所有文本以进行本地化
- c++ - 如何为此方法创建重载?
- encryption - 加载 ECIES 公钥
- python - 按“总和”权重降序对 PIVOT 表进行排序
- javascript - 为什么 document.querySelector 返回一个 Element 而 document.querySelectorAll 返回一个 NodeList
- python - 为什么我得到多个输出?