2017-05-09 47 views
0

我在我的textarea以下文本使用Javascript/jQuery的獲取從textarea的字符串第100個字與斷行

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

,但是當我安慰extractedText的長度,它返回165,而不是100這是不對的,任何一個可以請找到我在哪裏犯錯。

+0

你試圖通過空間和換行,而不是使用一個正則表達式來分割? – Mokkun

+0

不,我沒有拆分文本,只是通過正則表達式提取。 –

+0

我添加了一個在空間上使用拆分的答案 – Mokkun

回答

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>

+0

好吧,我想在文本中保留換行符,您的代碼看起來像會從字符串中刪除所有換行符。 –

+0

@MansoorAkhtar現在看看片段 –

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

,我想保留文本中的換行符。 –

0

你可以試試這個。

var extractedText = text.split(' ').slice(0,100).join(' '), 
+0

好吧,我想保留文本中的換行符。 –

0

請參閱代碼below..hope這將有助於..

$(document).ready(function() { 
 
\t \t \t 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>

相關問題