我正在使用一个新闻 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"