Uzun bir tablomuz var ve ben ilk 4 kelimeyi göstermek istiyorum., html içinde bir deyimin ilk 4 sözcüğünü görüntüler
örnek:
The style of this paper is a combination of an external stylesheet, and internal style.
böyle olur:
bu tarzı ...
Uzun bir tablomuz var ve ben ilk 4 kelimeyi göstermek istiyorum., html içinde bir deyimin ilk 4 sözcüğünü görüntüler
örnek:
The style of this paper is a combination of an external stylesheet, and internal style.
böyle olur:
bu tarzı ...
Bunun için normal bir ifade kullanabilirsiniz çağırabilir. Bu
var text = 'The style of this paper is a combination of an external stylesheet, and internal style.';
document.write(text.replace(/^((\w*\W*){0,4}).*/, '$1...'));
kullanım
strArr[] = str.split(" ");
str[0]
, 012., str[2]
, str[3]
ne kullanmak istediğiniz sonucudur.
Bu gerçekten soruya cevap vermiyor. .. –
Bir öğenin metninden ilk N kelimeyi döndüren bir işlev oluşturabilirsiniz. Örneğin: sarıcı
<div id="foo">This is a sample text!</div>
olarak örneğin kimliği "foo" varsa
function firstWords(wrapperId, wordCount) {
var element = document.getElementById(wrapperId);
var textArray = element.textContent.split(/\s/);
if(textArray.length >= wordCount) {
return textArray.slice(0, wordCount).join(" ");
} else {
return textArray.join(" ");
}
}
Sonrasında
console.log(firstWords("foo", 4));
size yardımcı olabilir http://stackoverflow.com/questions/36442069/how-to-reduce-the-length-of ilk dört kelimeleri alması ve bazı noktalar ekler paragraf-by-nokta –
Bakın: http://stackoverflow.com/questions/5956610/how-to-select-first-10-words-of-a-sentence – Lifka