Javascript-форум (https://javascript.ru/forum/)
-   Javascript под браузер (https://javascript.ru/forum/css-html/)
-   -   Выбрать предложение до второй точки. Регулярные выражения. (https://javascript.ru/forum/css-html/78090-vybrat-predlozhenie-do-vtorojj-tochki-regulyarnye-vyrazheniya.html)

flyte 25.07.2019 00:37

Выбрать предложение до второй точки. Регулярные выражения.
 
Доброго времени суток. Помогите составить регулярное выражения для выбора первых двух предложений из текста.
English singer, songwriter, guitarist, record producer, and actor... He attended the Academy of Contemporary Music in Guildford as an undergraduate from the age of 18 in 2009. In early 2011, Sheeran independently released the extended play, No. 5 Collaborations Project. After signing with Asylum Records, his debut album, +, was released in September 2011. It topped the UK and Australian charts, reached number five in the US, and has since been certified seven-times platinum in the UK. The album contains the single "The A Team", which earned him the Ivor Novello Award for Best Song Musically and Lyrically...

Я думал выбрать до 2-й точки, но после первого предложения может быть троеточие и такой вариант сразу отпал

ksa 25.07.2019 07:51

Цитата:

Сообщение от flyte
составить регулярное выражения для выбора первых двух предложений из текста

Как вариант...

var txt='English singer, songwriter, guitarist, record producer, and actor... ';
txt+='He attended the Academy of Contemporary Music in Guildford as an undergraduate ';
txt+='from the age of 18 in 2009. In early 2011, Sheeran independently released ';
txt+='the extended play, No. 5 Collaborations Project. After signing with Asylum ';
txt+='Records, his debut album, +, was released in September 2011. It topped the ';
txt+='UK and Australian charts, reached number five in the US, and has since been ';
txt+='certified seven-times platinum in the UK. The album contains the single ';
txt+='"The A Team", which earned him the Ivor Novello Award for Best Song Musically ';
txt+='and Lyrically...';
alert(txt.match(/([^\.]+\.{1,3}\s?){1,2}/)[0]);

ksa 25.07.2019 07:56

Для других вариантов окончания предложения...
var txt='English singer, songwriter, guitarist, record producer, and actor... ';
txt+='He attended the Academy of Contemporary Music in Guildford as an undergraduate ';
txt+='from the age of 18 in 2009! In early 2011, Sheeran independently released ';
txt+='the extended play, No. 5 Collaborations Project. After signing with Asylum ';
txt+='Records, his debut album, +, was released in September 2011. It topped the ';
txt+='UK and Australian charts, reached number five in the US, and has since been ';
txt+='certified seven-times platinum in the UK. The album contains the single ';
txt+='"The A Team", which earned him the Ivor Novello Award for Best Song Musically ';
txt+='and Lyrically...';
alert(txt.match(/([^\.!?]+([!?]|(\.{1,3}))\s?){1,2}/)[0]);

Rise 25.07.2019 17:49

flyte, ksa, в английском точка еще используется в дробных числах.

ksa 26.07.2019 07:28

Rise, я даже не расстроился... :D

var txt='English singer, songwriter, guitarist, record producer, and actor... ';
txt+='He attended the Academy of Contemporary Music in Guildford as an undergraduate ';
txt+='from the age of 18.21 in 2009! In early 2011, Sheeran independently released ';
txt+='the extended play, No. 5 Collaborations Project. After signing with Asylum ';
txt+='Records, his debut album, +, was released in September 2011. It topped the ';
txt+='UK and Australian charts, reached number five in the US, and has since been ';
txt+='certified seven-times platinum in the UK. The album contains the single ';
txt+='"The A Team", which earned him the Ivor Novello Award for Best Song Musically ';
txt+='and Lyrically...';
txt=txt.replace(/(\d*)\.(\d+)/g,'$1,$2');
txt=txt.match(/([^\.!?]+([!?]|(\.{1,3}))\s?){1,2}/)[0];
txt=txt.replace(/(\d*)\,(\d+)/g,'$1.$2');
alert(txt);

flyte 29.07.2019 13:16

ksa,
спасибо большое


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