为了账号安全,请及时绑定邮箱和手机立即绑定

如何从字符串中删除动态声部?

如何从字符串中删除动态声部?

月关宝盒 2021-04-07 17:23:26
您好我是新人请放纵:)我正在做一个带有很多动态值的标签生成器,这是最终的输出:1554710490545:[B @ 773c0cef:{“ header”:{“ eventId”:“ c0afad22-6472-4f23-9a5d-1c5e0b48024e ”,“ tmst”:“ 1554710490545 ”,“ agencyId”:“ tazz ”,“ customerId”:“ 02c “,” type“:” EzNavigationHitPayload“},” trackingEzNavHit“:{” hitBase“:{” hitId“:” 8735b582-2be7-4764-ac83-da0e4d2206df “,” ezakusBid“:” a40afeb4-066f-4dc8-9de6 -e450facecee9 “,” tmst“:” 1554710490545 “,” wid“:” widtest “,” cookieOk“:true,” userAgent“:”Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 73.0.3683.86 Safari / 537.36“,” ipV4“:” 77.154.199.100 “,” ezCustom“:{” eznode“: ”gfnode-001 “,”版本“:” 4.0.10 “},” agencyId“:” tazz “,” customerId“:” 02c “},” ezVarious“:{ ” ignore“:” STRING_TO_CHECK“ }}}我只想保留这部分:{“ hitId”:“ 8735b582-2be7-4764-ac83-da0e4d2206df”,“ ezakusBid”:“ a40afeb4-066f-4dc8-9de6-e450facecee9”,“ tmst”:“ 1554710490545”,“ wid”:“ widtest”,“ cookieOk“:true,” userAgent“:” Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,like Gecko)Chrome / 73.0.3683.86 Safari / 537.36“,” ipV4“:” 77.154.199.100“ ,“ ezCustom”:{“ eznode”:“ gfnode-001”,“ version”:“ 4.0.10”},“ agencyId”:“ tazz”,“ customerId”:“ 02c”},“ ezVarious”:{“忽略“:” STRING_TO_CHECK“}}}所有以粗体显示的值都是动态生成的,并且每次都会更改。我尝试使用正则表达式,但似乎很困难
查看完整描述

3 回答

?
料青山看我应如是

TA贡献1772条经验 获得超8个赞

从第一个花括号开始{,所有内容似乎都是JSON。


所以你可以使用之后获取字符串


let str = `1554710490545:[B@773c0cef:{"header":{"eventId":"c0afad22-6472-4f23-9a5d-1c5e0b48024e", "tmst":"1554710490545","agencyId":"tazz","customerId":"02c", "type":"EzNavigationHitPayload"},"trackingEzNavHit": {"hitBase":{"hitId":"8735b582-2be7-4764-ac83-da0e4d2206df", "ezakusBid":"a40afeb4-066f-4dc8-9de6-e450facecee9","tmst":"1554710490545", "wid":"widtest","cookieOk":true, "userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36","ipV4":"77.154.199.100","ezCustom": {"eznode":"gfnode-001","version":"4.0.10"},"agencyId":"tazz","customerId":"02c"}, "ezVarious":{"ignore":"STRING_TO_CHECK"}}}`;


let x = str.match(/({.*})/);

console.log(JSON.parse(x[1]).trackingEzNavHit)


查看完整回答
反对 回复 2021-04-22
?
GCT1015

TA贡献1827条经验 获得超4个赞

这是json数据。因此,您可以根据需要通过键访问值。例如,首先取空白json


 let output={}

 output["hintId"]= data.hintId

 output["ezakusBid"] = data.ezakusBid


And you can create new json data as your wish


查看完整回答
反对 回复 2021-04-22
?
跃然一笑

TA贡献1826条经验 获得超6个赞

如果您可以访问JSON,请使用它,而不是解析字符串...trackingEzNavHit.hitBase如果首键有时会不同,这是一种动态访问的方法。


const json = {

  "1554710490545": {

    "B@773c0cef": {

      "header": {

        "eventId": "c0afad22-6472-4f23-9a5d-1c5e0b48024e",

        "tmst": "1554710490545",

        "agencyId": "tazz",

        "customerId": "02c",

        "type": "EzNavigationHitPayload"

      },

      "trackingEzNavHit": {

        "hitBase": {

          "hitId": "8735b582-2be7-4764-ac83-da0e4d2206df",

          "ezakusBid": "a40afeb4-066f-4dc8-9de6-e450facecee9",

          "tmst": "1554710490545",

          "wid": "widtest",

          "cookieOk": true,

          "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",

          "ipV4": "77.154.199.100",

          "ezCustom": {

            "eznode": "gfnode-001",

            "version": "4.0.10"

          },

          "agencyId": "tazz",

          "customerId": "02c"

        },

        "ezVarious": {

          "ignore":"STRING_TO_CHECK"

        }

      }

    }

  }

};


console.log(json[Object.keys(json)[0]][Object.keys(json[Object.keys(json)[0]])].trackingEzNavHit.hitBase);


查看完整回答
反对 回复 2021-04-22
  • 3 回答
  • 0 关注
  • 157 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信