Как в списке удалить весь текст выше 3-рёх подряд строк заканчивающихся на знаки препинания в конце?

Есть переменная [[LIST_WITH_FILE_CONTENT]] в ней много строчек.

Подскажите, как с помощью JS удалить в нём все строки что выше 3-рёх подряд идущих строк.

В конце этих 3-рёх строк должны стоять символы из списка [[SYMBOLS]] .

В списке [[SYMBOLS]] находятся знаки препинания: . , ! ? " ' : ” “

Пример [[LIST_WITH_FILE_CONTENT]] : Or to take arms against a sea of troubles? And by opposing end them. To die—to sleep No more; and by a sleep to say we end. The heart-ache and the thousand natural shocks That flesh is heir to: ’tis a consummation! Devoutly to be wish’d. To die, to sleep; To sleep, perchance to dream—ay, there’s the rub: For in that sleep of death what dreams may come Borne on the bier6 with white and bristly beard: Then of thy beauty do I question make

Должно остаться так (выделил красной рамкой): image


Ответы (3 шт):

Автор решения: Citizen_39895
function qwe(LIST_WITH_FILE_CONTENT) {
    let splittedList = LIST_WITH_FILE_CONTENT.split('\n').reverse();

    if (splittedList.length < 3) {
        return LIST_WITH_FILE_CONTENT;
    }

    let resultList = [];
    const SYMBOLS = `.,!?"':”“;`;

    for (let i = 0; i < splittedList.length - 2; i) {
        let currentLine = splittedList[i];
        let secondLine = splittedList[i + 1];
        let thirdLine = splittedList[i + 2];

        if (SYMBOLS.includes(currentLine[currentLine.length - 1])) {
            resultList.push(currentLine);

            if (SYMBOLS.includes(secondLine[secondLine.length - 1])) {
                resultList.push(secondLine);

                if (SYMBOLS.includes(thirdLine[thirdLine.length - 1])) {
                    resultList.push(thirdLine);
                    break;
                } else {
                    resultList.push(thirdLine);
                    i += 3;
                }
            } else {
                resultList.push(secondLine);
                i += 2;
            }
        } else {
            resultList.push(currentLine);
            i += 1;
        }
    }

    return resultList.reverse().join('\n');
}
→ Ссылка
Автор решения: Laukhin Andrey

Счетчик - сила!

let text = `Or to take arms against a sea of troubles?
And by opposing end them. To die—to sleep
No more; and by a sleep to say we end.
The heart-ache and the thousand natural shocks
That flesh is heir to: ’tis a consummation!
Devoutly to be wish’d. To die, to sleep;
To sleep, perchance to dream—ay, there’s the rub:
For in that sleep of death what dreams may come
Borne on the bier6 with white and bristly beard:
Then of thy beauty do I question make`;

let symbols = /[.,!?"':;”“]/;

function process(text)
{
  let counter = 0;

  return text.split('\n').reverse().filter(function(v) {
    if (counter == 3) return false;
    else {
      counter = symbols.test(v.slice(-1)) ? counter + 1 : 0;
      return true;
    }
  }).reverse().join('\n');
}

console.log(process(text));

→ Ссылка
Автор решения: Mikhail Eliseev

@laukhin-andrey Содержание переменной [[LIST_WITH_FILE_CONTENT]] не поменялось(

let symbols = [[SYMBOLS]];
let listcontent = [[LIST_WITH_FILE_CONTENT]]; 
function process(text)
{
  let counter = 0;

  return text.split('\n').reverse().filter(function(v) {
    if (counter == 3) return false;
    else {
      counter = symbols.test(v.slice(-1)) ? counter + 1 : 0;
      return true;
    }
  }).reverse().join('\n');
}
[[LIST_WITH_FILE_CONTENT]]=listcontent;

→ Ссылка