.subString 直到最后一个句子结束



我正在使用新闻API,由于字符限制,该API部分回复了截断的文本。它们以"..."、"[...]"结尾或没有任何指示。

我想切断最后一句之后的所有内容,所以我有干净的文本。

这是我当前的代码:

var shortenText = function (input) {
        if (input.search(/w+./) > 0) {
            return input.substring(0, input.lastIndexOf('.') + 1);
        } else {
            return input
        }
    }

问题是,日期(2 月 2 日)、数字 (2.000) 或美国搞砸了我的输出。

我需要像lastIndexOf(/\w+.\w+./g)这样的东西来找到最后两个单词,然后用句号来确定一个句子的结尾。但是lastIndexOf不适用于正则表达式。:/

任何想法如何解决这个问题?

谢谢!

编辑:以下是 API 响应中的一些示例:

"description": "The Boston-based investment company, Fidelity, has just confirmed rumors about a new cryptocurrency trading platform and custodian service for institutional investors. The firm has also announced that the project is in the final testing phase. In October 2018, the company set up FDAS (Fidelity Digital Asset Services) — a subsidiary dedicated to working with digital […]"
"description": "The Chicago Board Options Exchange (CBOE) and VanEck refiled their Bitcoin exchange-traded fund (ETF) application. But, there likely won't be a Bitcoin ETF by the end of the year and possibly by the year's end. On January 31, as CCN reported, VanEck announced that it submitted its newly drafted Bitcoin ETF application to the U.S. Securities and Exchange Commission (SEC). The VanEck SolidX...</p>"
"description": "The Securities and Exchanges Commission is looking for a blockchain analysis firm to provide readable crypto data according to a new tender notice. It says: “The U.S. Securities and Exchange"

好的,我将简单地使用这个插件:https://github.com/Tessmore/sbd

不是最漂亮的方式,但它会做的。

最新更新