我有 JSONL 檔案作為文本 - 字串,它是一個非常大的檔案,對于轉換為標準 JSON 沒有用:
{"id":"gid:\/\/shopify\/ProductVariant\/32620848382088","__parentId":"gid:\/\/shopify\/Product\/4632300847240"}
{"id":"gid:\/\/shopify\/Product\/4632300912776"}
{"namespace":"daily_deals","key":"status","value":"inactive","__parentId":"gid:\/\/shopify\/Product\/4632300912776"}
{"namespace":"daily_deals","key":"endtime","value":"1604966400000","__parentId":"gid:\/\/shopify\/Product\/4632300912776"}
{"id":"gid:\/\/shopify\/ProductVariant\/32620848447624","__parentId":"gid:\/\/shopify\/Product\/4632300912776"}
{"id":"gid:\/\/shopify\/Product\/4632301011080"}
{"namespace":"daily_deals","key":"status","value":"inactive","__parentId":"gid:\/\/shopify\/Product\/4632301011080"}
{"namespace":"daily_deals","key":"endtime","value":"1604966400000","__parentId":"gid:\/\/shopify\/Product\/4632301011080"}
{"id":"gid:\/\/shopify\/ProductVariant\/32620848808072","__parentId":"gid:\/\/shopify\/Product\/4632301011080"}
{"id":"gid:\/\/shopify\/ProductVariant\/39402297720968","__parentId":"gid:\/\/shopify\/Product\/4632301011080"}
{"id":"gid:\/\/shopify\/Product\/4673135444104"}
我想在前端解決問題,所以我需要使用 javascript 。如何使用正則運算式僅選擇包含文本的行:"gid://shopify/Product/4632301011080"和"namespace":"daily_deals"?所以如果包含文本,我需要從 { 到 } 的整行
使用正則運算式或其他技術的最佳解決方案是什么?請建議?文本 JSONL 檔案平均為 10mb,所以我認為它不會對瀏覽器記憶體產生很大影響。
更新:
我想搜索的所有行都以{"namespace":
和其他一次我想因為性能而忽略
uj5u.com熱心網友回復:
/gid://shopify/Product/\d{1,}/
uj5u.com熱心網友回復:
你可以使用這個正則運算式:
/^{"namespace":"daily_deals".*?"gid:\/\/shopify\/Product\/4632301011080".*/gm
let content = `{"id":"gid:\/\/shopify\/ProductVariant\/32620848382088","__parentId":"gid:\/\/shopify\/Product\/4632300847240"}
{"id":"gid:\/\/shopify\/Product\/4632300912776"}
{"namespace":"daily_deals","key":"status","value":"inactive","__parentId":"gid:\/\/shopify\/Product\/4632300912776"}
{"namespace":"daily_deals","key":"endtime","value":"1604966400000","__parentId":"gid:\/\/shopify\/Product\/4632300912776"}
{"id":"gid:\/\/shopify\/ProductVariant\/32620848447624","__parentId":"gid:\/\/shopify\/Product\/4632300912776"}
{"id":"gid:\/\/shopify\/Product\/4632301011080"}
{"namespace":"daily_deals","key":"status","value":"inactive","__parentId":"gid:\/\/shopify\/Product\/4632301011080"}
{"namespace":"daily_deals","key":"endtime","value":"1604966400000","__parentId":"gid:\/\/shopify\/Product\/4632301011080"}
{"id":"gid:\/\/shopify\/ProductVariant\/32620848808072","__parentId":"gid:\/\/shopify\/Product\/4632301011080"}
{"id":"gid:\/\/shopify\/ProductVariant\/39402297720968","__parentId":"gid:\/\/shopify\/Product\/4632301011080"}
{"id":"gid:\/\/shopify\/Product\/4673135444104"}`;
let result = content.match(/^{"namespace":"daily_deals".*?"gid:\/\/shopify\/Product\/4632301011080".*/gm);
console.log(result);
uj5u.com熱心網友回復:
試試這個:假設"namespace":"daily_deals"
部分總是在"gid://shopify/Product/4632301011080"
這個正則運算式起作用之前出現。
^{"namespace":"daily_deals".*"gid:\/\/shopify\/Product\/4632301011080".*
見現場演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/531036.html