我的textarea
中有以下文字var text = "asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er";
现在我想用jquery / javascript从该字符串中提取前100个单词, 这是我从字符串中提取单词的代码。
var wordLimit = 100;
var re ='(([^\\s]+\\s\\s*){' + wordLimit + '})(.*)';
var extractedText = text.replace(new RegExp(re, "gm"), '$1');
console.log(extractedText.match(/\S+/g).length);
但是当我控制extractText的长度时,它返回165而不是100,这是错误的,任何人都可以找到我犯错的地方。
答案 0 :(得分:1)
这应该对你有帮助。
函数getWords()
返回x个单词。我刚刚在其末尾添加了.split(" ").length
,以便您可以看到它返回的字词数量。
var text = $("#text").val()
var wordLimit = 100;
function getWords(str) {
return str.split(/\s+/).slice(0,wordLimit).join(" ");
}
console.log("we have " + text.split(" ").length + " words")
console.log("getWords returned " + getWords(text).split(" ").length + " words");
$("#result").val(text.substring(0,getWords(text).length))
textarea{
height:300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="text">asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we</textarea>
<textarea id="result"></textarea>
答案 1 :(得分:0)
尝试:
//replaces newlines with spaces
text = text.replace(/(?:\r\n|\r|\n)/g, ' ');
array = text.split(' ');
for(i=0;i<word_limit;i++) {
//print 100 first words in console
console.log(array[i]);
}
编辑:
好吧,我也希望保留文本中的换行符。
var text = "asdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw er\nasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw er\nasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw er\nasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw erasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw erasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw erasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw er\nasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw er\nasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw er\nasdf wr we rwe rwe rw er\nwer\nwer\nwer\nwe\nr wer we we rwe rw er";
var max = 100;
var array = text.split('\n');
var count = 0;
var new_array = [];
var last = [];
for(i=0;i<array.length;i++) {
console.log(array[i].split(' ').length);
count += array[i].split(' ').length;
new_array.push(array[i]);
if(count>=max) {
to_remove = count - max;
index = new_array[new_array.length-1].split(' ').length - to_remove;
for(j=0;i<index;j++) {
last.push(new_array[new_array.length-1].split(' ')[j]);
}
new_array[new_array.length-1] = last.join(' ');
break;
}
}
console.log(new_array.join('\n'));
total = 0;
for(i=0;i<new_array.length;i++) {
total += new_array[i].split(' ').length;
}
console.log('total '+total);
答案 2 :(得分:0)
你可以试试这个。
var extractedText = text.split(' ').slice(0,100).join(' '),
答案 3 :(得分:0)
请参考下面的代码..希望这会有所帮助..
$(document).ready(function () {
var text = $("#checkval").text().length;
var limitword = $("#checkval").text().substr(0,100);
alert(limitword);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="checkval">asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw erasdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er
asdf wr we rwe rwe rw er
wer
wer
wer
we
r wer we we rwe rw er</textarea>