0

JavaScript と jquery を使用して、文を分離し、それらを 1 つずつ配列に格納しようとしています。これは私が試したものです

var sentenceArray = new Array();

//SPLITS PARAGRAPHS INTO SENTENCES//
$('#text').each(function() {

    var sentences = $(this).html().replace(/([^.!?]*[^.!?\s][.!?]['"]?)(\s|$)/g,'<span class="sentence">$1</span>$2');
                    sentenceArray.push(sentences);

});

ただし、これは、配列の [0] インデックスに結合されたすべての文を返します。それらをどのように分離しますか?

<div id="text">

    Crowds blocked main roads in Sao Paulo and Brasilia, and protesters confronted police in Rio de Janeiro state shortly after the U-turn was announced.
    Earlier, there were clashes before Brazil's football team played Mexico in Fortaleza in the Confederations Cup.
    Protesters are angry at corruption and high spending on next year's World Cup.
    Activists say they have not changed their intention to hold the biggest demonstrations yet on Thursday.
    The BBC's Julia Carneiro, in Sao Paulo, says hundreds of thousands are expected on the streets there before another round of matches in the Confederations Cup.

</div>
4

2 に答える 2

0

配列オブジェクトが正しく定義されていません。例としてこれを試してください:

   var array = [];
   array.push('one');
   array.push('two');
   alert(array[0]);
   alert(array[1]);
于 2013-06-20T07:49:25.817 に答える
0

あなたはこのようにすることができます、見てください:

var sentenceArray = new Array();

//SPLITS PARAGRAPHS INTO SENTENCES//
var sentences = $(this).html().replace(/([^.!?]*[^.!?\s][.!?]['"]?)(\s|$)/g,'<span class="sentence">$1</span>$2<SENTENCE_END>');

sentenceArray = sentences.split("<SENTENCE_END>");

正規表現を変更できる場合は可能です。

于 2013-06-20T07:39:05.163 に答える