简体   繁体   中英

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

Using javascript and jquery i'm trying to separate sentences and store them one by one into an array. This is what I've tried

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

});

This, though, returns all the sentences combined into the [0] index of the array. How do I separate them?

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

You can do it like this , look:

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

It will be possible in case you're able to make changes to your regular expression.

Your array object is defined incorrectly. Try this as an example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM