Created
October 30, 2014 09:36
-
-
Save rubenCodeforges/c77b32d794272051e797 to your computer and use it in GitHub Desktop.
JS create excerpt example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var string = "some text here with some amount of chars lets rteplaece it like a excerpt"; | |
var description = getExcerpt(string,10); | |
// принимает строку и лимит после скольки знаков искать пробел и обрезать | |
function getExcerpt( str, limit ){ | |
var fullText = str; | |
var shortText = str; | |
shortText = shortText.substr( 0, shortText.lastIndexOf( ' ', limit ) ) + '...'; | |
var returnString = { | |
fullText: fullText, | |
shortText: shortText | |
}; | |
return returnString; | |
} | |
// функция возвратит объект , свойство shortText короткое описание обрезанное по limit и пробелу fullText полный текст | |
console.log(description.shortText); | |
console.log(description.fullText); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment