React js creating json file with reading to wrap only after the final point of the sentence

React js creating json file with reading to wrap only after final point of the sentence

文件:

WEBVTT

00:00:00.000 --> 00:00:12.920
Hi.
Hi.

00:00:26.040 --> 00:00:26.960
Hi.

00:00:26.040 --> 00:00:26.960
Hi.Hi

00:00:26.040 --> 00:00:26.960
Hi..Hi

00:00:26.040 --> 00:00:26.960
Hi...Hi.

我的结果json:

[
    {
        "timeString": "00:00:00.000 --> 00:00:12.920",
        "from": "00:00:00.000",
        "to": "00:00:12.920",
        "string": "Hi.\nHi."
    },
    {
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi."
    },
    {
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi.Hi"
    },
    {
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi..Hi"
    },
    {
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi...Hi."
    }
]

Json 结果预期:

[
    {
        "timeString": "00:00:00.000 --> 00:00:12.920",
        "from": "00:00:00.000",
        "to": "00:00:12.920",
        "string": "Hi.\nHi."
    },
    {
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi."
    },
    {
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi.\nHi" <- different
    },
    {
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi..\nHi" <- different
    },
    {
        "timeString": "00:00:26.040 --> 00:00:26.960",
        "from": "00:00:26.040",
        "to": "00:00:26.960",
        "string": "Hi...\nHi." <- different
    }
]

代码:

export default function App() {
  fetch("/file.txt")
    .then((r) => r.text())
    .then((text) => {
      const v = text
        .replace("WEBVTT", "")
        .replace(/[\r\n]{2,}/g, "\n")
        .replace("\n", "");
      const lines = v.split("\n");

      let inc = -1;
      const sub = lines.reduce((acc, d, index, array) => {
        const test = new RegExp("\b(\d{2}:\d{2}:\d{2})\.(\d{3})\b").test(
          d
        );
        if (test) {
          inc++;
          const a = d.split("-->").filter((e) => e !== "-->");
          acc.push({ timeString: d, from: a[0].trim(), to: a[1].trim() });
        } else {
          let a = acc[inc]?.string;
          if (a !== undefined) a += `\n${d}`;
          else a = d;
          acc[inc] = { ...acc[inc], string: a };
        }
        return acc;
      }, []);
      console.log(sub);
    });

  return <div className="App"></div>;
}

我想得到的结果是,每当在文件中作为文本,所以字符串字段中的一个单词或短语以句号结尾,它被放在\n中,如我的结果所示希望,但必须考虑以下情况。

  1. 如果点后没有任何东西那么你不需要把\n.
  2. 如果在句点之后还有另一个句点,你必须把 \n 放在最后一个句点之后,例如当有两个或三个暂停点时(如我考虑的最后两种情况文件)。

Link: https://codesandbox.io/s/zealous-robinson-ov34m?file=/src/App.js

你能帮帮我吗?

Negative lookaheads可能是一个解决方案。

以下将单个 . 的序列替换为 .\n,但仅当它后面没有任何 . 或新行 (\n) 时,并且不后跟序列结束符 ($):

const finder = /\.(?![\n.])(?!$)/g;
const mangle = (str) => str.replace(finder, '.\n');

console.log(mangle('Hi.\nHi.')); // "Hi.\nHi."
console.log(mangle('Hi.'));      // "Hi."
console.log(mangle('Hi.Hi'));    // "Hi.\nHi"
console.log(mangle('Hi..Hi'));   // "Hi..\nHi"
console.log(mangle('Hi...Hi.')); // "Hi...\nHi."