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

时间:2019-04-08 08:25:59

标签: javascript reactjs

你好,我是新人,请放纵:)

我正在做一个带有很多动态值的标签生成器,这是最终的输出:

  

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”:“ 最疯狂”,“ cookieOk”:是的,   “ 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 ”,“ version”:“ 4.0.10 ”},“ agencyId”:“ tazz ” ,“ customerId”:“ 02c ”},   “ ezVarious”:{“忽略”:“ 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,例如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”}}}

所有以粗体显示的值都是动态生成的,并且每次都会更改。 我尝试使用正则表达式,但似乎很困难

3 个答案:

答案 0 :(得分:0)

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

 let output={}
 output["hintId"]= data.hintId
 output["ezakusBid"] = data.ezakusBid

And you can create new json data as your wish

答案 1 :(得分:-1)

如果您有权访问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);

答案 2 :(得分:-2)

从第一个花括号{开始,所有内容似乎都是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)