簡體   English   中英

如何將各個類依次推入數組?

[英]How do I .push separate classes into an array one after another?

我正在嘗試使用javascript和jquery分隔句子並將它們一一存儲到數組中。 這就是我嘗試過的

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>

您可以這樣做,請看:

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>");

如果您能夠對正則表達式進行更改,則有可能。

您的數組對象定義不正確。 以此為例:

   var array = [];
   array.push('one');
   array.push('two');
   alert(array[0]);
   alert(array[1]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM