我将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。如何使用regex只选择包含文本的行:"gid://shopify/Product/4632301011080"和"namespace":"daily_deals"?所以我需要整行从{到}如果包含文本
是使用regex或其他技术的最佳解决方案吗?请建议一下?文本JSONL文件平均为10 it,因此我认为它不会对浏览器内存产生很大影响。
更新:由于性能原因,我想搜索的所有行都以{"namespace":和其他我想忽略的行开始
发布于 2022-11-09 22:09:53
您可以使用这个regex:
/^{"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);
发布于 2022-11-09 21:57:55
试一试:假设"namespace":"daily_deals"部件总是在"gid://shopify/Product/4632301011080"之前出现,这个正则表达式将起作用。
^{"namespace":"daily_deals".*"gid:\/\/shopify\/Product\/4632301011080".*见现场演示。
发布于 2022-11-09 22:45:37
/gid://shopify/Product/\d{1,}/https://stackoverflow.com/questions/74381687
复制相似问题