0

I'm using a news API, which partly replies with cut off text, due to character restriction. They end with '...', '[...]' or without any indication.

I want to cut off everything after the last sentence, so I have clean text.

This is my current code:

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

The problem is, that dates (2. Feb.), numbers (2.000) or the U.S. mess up my output.

I'd need something like lastIndexOf(/\w+.\w+./g) to find the last two word, followed by a full stop to determine a sentence ended. But lastIndexOf doesn't work with Regex. :/

Any Ideas how to solve this?

Thanks!

Edit: Here are some examples from API response:

"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"
Arti
  • 1
  • 1
  • 2

1 Answers1

0

Ok, I'll simply go with this plugin: https://github.com/Tessmore/sbd

Not the prettiest way, but it will do.

Arti
  • 1
  • 1
  • 2