2016-04-11 15 views
0

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

+1

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 –

+0

Bakın: http://stackoverflow.com/questions/5956610/how-to-select-first-10-words-of-a-sentence – Lifka

cevap

2

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

-2

kullanım

strArr[] = str.split(" "); 

str[0], 012., str[2], str[3] ne kullanmak istediğiniz sonucudur.

+0

Bu gerçekten soruya cevap vermiyor. .. –

0

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)); 
İlgili konular