Javascript Word Count Disabled

I have a div with the id "shortblogpost". I would like to count to the 27th word, and then stop and add "..." at the end.

I tried the following code. The problem is counting letters, not words. I think it uses jQuery and does not spill JavaScript?

I need to use JavaScript only for different server reasons

<script type="text/javascript"> var limit = 100, text = $('div.shortblogpost').text().split(/\s+/), word, letter_count = 0, trunc = '', i = 0; while (i < text.length && letter_count < limit) { word = text[i++]; trunc += word+' '; letter_count = trunc.length-1; } trunc = $.trim(trunc)+'...'; console.log(trunc); </script> 

Ty well in advance for any help.

+4
source share
5 answers

Truncation function.

Usage: truncate ("This is a test of this function", 2); Returns: This is ...

Usage: truncate ("This is a test of this function", 5, "+++"); Refunds: This is a test +++

 function truncate (text, limit, append) { if (typeof text !== 'string') return ''; if (typeof append == 'undefined') append = '...'; var parts = text.split(' '); if (parts.length > limit) { // loop backward through the string for (var i = parts.length - 1; i > -1; --i) { // if i is over limit, drop this word from the array if (i+1 > limit) { parts.length = i; } } // add the truncate append text parts.push(append); } // join the array back into a string return parts.join(' '); } 

Edit: Quick and dirty execution according to OP parameters:

 <script type="text/javascript"> // put truncate function here... var ele = document.getElementById('shortblogpost'); ele.innerHTML = truncate(ele.innerHTML, 20); </script> 
+7
source

This can be done in one line of code:

 myString.replace(/(([^\s]+\s+){27}).+/, "$1..."); 

Or you can make it a function:

 function truncateString(s, wordCount) { var expr = new RegExp("(([^\\s]+\\s+){" + wordCount + "}).+"); return s.replace(expr, "$1..."); } 

So, to make this work for your code, you can do:

 var post = $('div.shortblogpost').text(); // get the text post = postText.replace(/(([^\s]+\s+){27}).+/, "$1..."); // truncate the text $('div.shortblogpost').text(post); // update the post with the truncated text 
+5
source

A cycle is added word by word, whereas (there are words && the number of letters below the limit). The only thing you need to do is replace the second condition with & & count words below the limit.

Converting this pseudo code to JS should be trivial ...

+1
source

How about this? jsfiddle

HTML:

 <div id='shortblogpost'>test test test test test test test test test test test</div> 

javascript:

 alert(document.getElementById('shortblogpost').innerHTML); var numWordToDisplay = 3; //how many words u want to display in your case 27 var newArray = document.getElementById('shortblogpost').innerHTML.split(' '); if(newArray.length >= numWordToDisplay ) newArray.length = numWordToDisplay; console.log(newArray.join(' ') + '...'); //test test test... 
+1
source

This should work:

 var words = $('div.shortblogpost').text().replace( /\s/g, ' ' ).split( ' ' ); var result = ""; for( var w = 0 ; w < 27 ; w++ ) { if( words[w].length > 0 ) { result += words[w] + ' '; } } result = result.trim() + "..."; 
0
source

Source: https://habr.com/ru/post/1339271/


All Articles