繁体   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