【问题标题】:.subString until last sentence ends.subString 直到最后一句结束
【发布时间】:2019-02-02 19:28:57
【问题描述】:

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

【问题讨论】:

  • 你能举例说明input 的样子和你想做什么
  • 怎么样,例如Foo bar. Baz...?从那以后,你想要Foo bar.吗?
  • 没有简单的方法可以做到这一点。以“约翰·肯尼迪”为例。
  • @CertainPerformance:正确,它应该删除剩余和不完整的文本。

标签: javascript regex substring lastindexof


【解决方案1】:

好的,我就直接使用这个插件:https://github.com/Tessmore/sbd

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

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2019-12-18
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多