Javascript-форум (https://javascript.ru/forum/)
-   Events/DOM/Window (https://javascript.ru/forum/events/)
-   -   Как в списке удалить весь текст выше 3-рёх подряд строк заканчивающихся на знаки преп (https://javascript.ru/forum/events/83880-kak-v-spiske-udalit-ves-tekst-vyshe-3-rjokh-podryad-strok-zakanchivayushhikhsya-na-znaki-prep.html)

mik888em 11.04.2022 19:56

Как в списке удалить весь текст выше 3-рёх подряд строк заканчивающихся на знаки преп
 
Вложений: 1
Есть переменная [[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


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

рони 11.04.2022 20:32

mik888em,
:-?
<script>
        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 redo = new Set();
        let regStr = /(^[\s\S]+\n)(?=(.*?[.,!?"':;”“]\n){3})/mg;
        function replace(a, b, c) {
            return '';
        }
        text = text.replace(regStr, replace);
        document.write(`<pre>${text}</pre>`)
    </script>

ksa 11.04.2022 20:57

Предложу такой вариант...
<textarea id='txt'>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</textarea>
<script>
let txt = document.querySelector('#txt').value
const re = /([^\n]*[!:;]\n){3}/
const n = txt.search(re)
txt = txt.substring(n, txt.length)
alert(txt)
</script>

рони 11.04.2022 21:24

ksa,
:)

ksa 11.04.2022 21:27

рони, я не поспеваю за тобой. :write:


Часовой пояс GMT +3, время: 06:23.