You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将jq正则筛选转换为if语句的布尔条件以删除指定字段?

解决方案

你只需要将test()正则匹配逻辑直接嵌入walk的if条件判断中,替换原有的精确匹配表达式即可,最终写法如下:

walk(if type == "object" and (.myfield | test("你的正则表达式")) then del(.xxx) else . end)

额外提示

  • 若要避免myfield字段不存在时出现报错,可以在条件中增加has("myfield")做前置校验:
    walk(if type == "object" and has("myfield") and (.myfield | test("你的正则表达式")) then del(.xxx) else . end)
    
  • test()的参数是字符串形式的正则表达式,比如要匹配包含my的内容,可写为test(".*my.*")

内容的提问来源于stack exchange,提问作者diemo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:37:33