使用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>
答案 0 :(得分: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>");
如果您能够对正则表达式进行更改,将会有可能。
答案 1 :(得分:0)
您的数组对象定义不正确。试试这个例子:
var array = [];
array.push('one');
array.push('two');
alert(array[0]);
alert(array[1]);