节点FS读取文件提取特定字符串

时间:2019-11-22 19:26:55

标签: node.js file readfile fs

嗨,这是我的问题,我有一个很大的网络日志,即使用chrome进行即时日志记录,其中包括了日志文件的一小部分以及需要提取的信息。

{"pid":2748,"tid":11624,"ts":345045004582,"ph":"I","cat":"devtools.timeline","name":"TimerInstall","s":"t","tts":175817,"args":{"data":{"timerId":3,"timeout":30000.0,"singleShot":true,"stackTrace":[{"functionName":"t.sendFetchRequest","scriptId":"12","url":"https://cvp.twitch.tv/2.17.2/wasmworker.min.js","lineNumber":1,"columnNumber":13925}]}}},
{"pid":2748,"tid":11624,"ts":345045004782,"ph":"I","cat":"devtools.timeline","name":"ResourceSendRequest","s":"t","tts":175969,"args":{"data":{"requestId":"2748.61","frame":"","url":"https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/CpkEmldXq0KjmFA-Mh5knkt1wdu0JKTUHWlvqmba3F2A20pXKynTgazmqtVdJFYUc_IgRCofliiy9UPE-FPDpwyfwOu6yjkomOty7YNCtE3Ax4myL0Z2va45aW5aPGMkn2ttEWxyUmrLVYC3EZ_e7Wxv6x1oHQB3RSUiiKjOYUBweG1nX3kUamZVsN4kmX5j7YZ3O8z1jAJpNGXNPrr1ADorCctWgefnAtrn4vgIbaiQaTaF0JXUfe7UII98YFpmwIiml6jVj2G9iRB25m2Uux6uMW47YnPGclgVKlLRWWutyJGTZsvco54cTqmzOSJO8drmAlJO3fjejbIXo9UIgHzHIt5DAqrleJ-VZek8L1-D6m9cpDWGGIx6JLedvbqy5uYHj2mhXqfZdlkSL_cq0JrgyDtGt8Y9UmHXERVPTGpGAo82x6EOepIc-XKUw83zdE4HtAjawn2NxJV0lIBa5GENTM1eh70PYVBZDD8x5y2DOkqbUl3iMjVgnCm1fmO-VihwajZJ2lzgy9A2pS683cTcWDw0eRuovRERQJ6rS45i2oJE0a0W4SZuziTCN8eqbWrXioh5nTVwbI7P1QIL7VBjK0552od_xq4fe9Xmxt_pRBfqiRabe4C6VbE_qsUCcLb0Yg_2RXxOESodx90kjW7ZaJxMlPcxk_koilhXudqn5GA3HIUfAmmwg7wQjL-iv2Rswq5PyVW1Z9x1EhBpkHOjQ9D6awqbs-jC0vwJGgxBHDSR_4FUU6cLBjs.m3u8","requestMethod":"GET","priority":"High","stackTrace":[{"functionName":"t.sendFetchRequest","scriptId":"12","url":"https://cvp.twitch.tv/2.17.2/wasmworker.min.js","lineNumber":1,"columnNumber":14017}]}}},
{"pid":2748,"tid":11624,"ts":345045005497,"ph":"X","cat":"v8.execute","name":"V8.HandleInterrupts","dur":5,"tdur":3,"tts":176545,"args":{}},
{"pid":2748,"tid":11624,"ts":345045005738,"ph":"I","cat":"devtools.timeline","name":"TimerRemove","s":"t","tts":176767,"args":{"data":{"timerId":2,"stackTrace":[{"functionName":"","scriptId":"12","url":"https://cvp.twitch.tv/2.17.2/wasmworker.min.js","lineNumber":1,"columnNumber":15401}]}}},
{"pid":2748,"tid":11624,"ts":345045005772,"ph":"E","cat":"v8.execute","name":"RunMicrotasks","tts":176793,"args":{"microtask_count":7}},

这是记录的数据的一小部分,但是在记录的json中,您将看到此行 https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/CpkEmldXq0KjmFA-Mh5knkt1wdu0JKTUHWlvqmba3F2A20pXKynTgazmqtVdJFYUc_IgRCofliiy9UPE-FPDpwyfwOu6yjkomOty7YNCtE3Ax4myL0Z2va45aW5aPGMkn2ttEWxyUmrLVYC3EZ_e7Wxv6x1oHQB3RSUiiKjOYUBweG1nX3kUamZVsN4kmX5j7YZ3O8z1jAJpNGXNPrr1ADorCctWgefnAtrn4vgIbaiQaTaF0JXUfe7UII98YFpmwIiml6jVj2G9iRB25m2Uux6uMW47YnPGclgVKlLRWWutyJGTZsvco54cTqmzOSJO8drmAlJO3fjejbIXo9UIgHzHIt5DAqrleJ-VZek8L1-D6m9cpDWGGIx6JLedvbqy5uYHj2mhXqfZdlkSL_cq0JrgyDtGt8Y9UmHXERVPTGpGAo82x6EOepIc-XKUw83zdE4HtAjawn2NxJV0lIBa5GENTM1eh70PYVBZDD8x5y2DOkqbUl3iMjVgnCm1fmO-VihwajZJ2lzgy9A2pS683cTcWDw0eRuovRERQJ6rS45i2oJE0a0W4SZuziTCN8eqbWrXioh5nTVwbI7P1QIL7VBjK0552od_xq4fe9Xmxt_pRBfqiRabe4C6VbE_qsUCcLb0Yg_2RXxOESodx90kjW7ZaJxMlPcxk_koilhXudqn5GA3HIUfAmmwg7wQjL-iv2Rswq5PyVW1Z9x1EhBpkHOjQ9D6awqbs-jC0vwJGgxBHDSR_4FUU6cLBjs.m3u8

我需要从记录的数据中提取此特定的url,它将始终以.m3u8文件扩展名结尾,并以https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/开头

感谢您的时间stackoverflow社区

1 个答案:

答案 0 :(得分:0)

这是我的解决方案

    if(line.includes('https://video-weaver.jfk04.hls.ttvnw.net/v1/playlist/')){
        const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/ig
        const matchedUrls = line.match(urlRegex);
        console.log(matchedUrls[0])
    }```