如何将 51.xx.xx.xx:33xxx:user:pass 格式化为 user:pass@51.xx.xx.xx:33xxx

How can I format 51.xx.xx.xx:33xxx:user:pass into user:pass@51.xx.xx.xx:33xxx

这是我目前所拥有的,但它 returns 只有一个代理,因为它重写了 x(无论代理多少)次。我不想创建一个新文件,而是用每个代理重写 proxies.txt。

const fs = require("fs");

const formatProxies = () => {
  const rawProxies = fs.readFileSync("./proxies.txt", "utf-8");
  const split = rawProxies.trim().split("\n");

  for (const p of split) {
    const parts = p.trim().split(":");
    const [ip, port, user, pass] = parts;
    fs.writeFileSync(
      "./proxies.txt",
      user + ":" + pass + "@" + ip + ":" + port + "\r\n",
      { encoding: "utf8" }
    );
  }
};
formatProxies();

这个有用吗?

const fs = require("fs");

const formatProxies = () => {
  const rawProxies = fs.readFileSync("./proxies.txt", "utf-8");
  const split = rawProxies.trim().split("\n");

  const lines = []
  for (const p of split) {
    const parts = p.trim().split(":");
    const [ip, port, user, pass] = parts;
    lines.push(user + ":" + pass + "@" + ip + ":" + port)
  }
  fs.writeFileSync(
    "./proxies.txt",
    lines.join("\r\n"),
    { encoding: "utf8" }
  );
};
formatProxies();

Node.js 有 fs.appendFileSync,它写入文件末尾而不是覆盖整个文件。