Как в списке удалить весь текст выше 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


Должно остаться так (выделил красной рамкой):
62545a1ed210b446705287.jpeg
  • Вопрос задан
  • 211 просмотров
Решения вопроса 2
XanXanXan
@XanXanXan
const matchIndex = str.search(/((.+)?[;,!?"':”“]\n){3}/);
const newStr =  str.slice((matchIndex > 0) ? matchIndex : 0);
Ответ написан
0xD34F
@0xD34F Куратор тега JavaScript
const punct = '.,!?;:"\'”“';
const numStrWithPunctEnd = 3;
const arr = str.split('\n');
const index = arr.findIndex(function(n, i, a) {
  return this.every(m => punct.includes(a[i + m]?.slice(-1)));
}, [...Array(numStrWithPunctEnd).keys()]);
const result = index !== -1 ? arr.slice(index).join('\n') : str;
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
const myTrim = (str) => {
    const splitN = str.split(/[\n]/g);
    const pattern = /[.|,|!|?|"|'|:|”|“]/;
    let repeatedInx = -1;
    let count = 0;

    for (const el of splitN) {
        repeatedInx += 1;
        
        if (pattern.test(el[el.length - 1])) {
            count += 1;
            continue;
        }
    
        if (count === 3) break;
        
        count = 0;
    }

    return splitN.slice(count && repeatedInx - count).join('\n');
};

myTrim(`
abc
abe
cde
ace?
ved.
cvd.
`);
//  'ace?\nved.\ncvd.\n'
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы