Hello Stack community,
I am trying to acheive a simple javascript function to first count the total number of words from a given string value.
After this, I wan't to store X number of words into an array to be able to loop with additionnal html elements arround it.
For exemple, I have this String :
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis feugiat sollicitudin lacus, nec pulvinar quam scelerisque at. Phasellus ut tellus scelerisque, fringilla ligula a, commodo odio. Aenean mattis eros sed purus iaculis, at pellentesque ligula luctus. Pellentesque luctus augue ut quam consequat rhoncus. In at dolor sed ipsum ullamcorper fermentum. Pellentesque porta convallis nisl vitae cursus. Maecenas luctus libero sit amet efficitur consequat. Suspendisse nec luctus dolor. Fusce sit amet scelerisque erat. Aenean ac tristique nisl. Etiam in est purus. In magna nunc, viverra nec ante quis, aliquam venenatis sem.
Here is the code I am actually working on. (I just took the required part).
// Var for Word max
var wordsMaxFirstPage = 40;
var wordsMaxOthersPages = 50;
// Default field (My String example)
var defaultField = $(this).find('.c_6937 .textarea .value');
// Count the number of words for this form
var countLengthOfCommentPage = defaultField.text().split(' ').length;
// Creat Array of the comment splits by Maximum words per page
var chunks = [];
for (var i = 0, charsLength = countLengthOfCommentPage; i < charsLength; i += wordsMaxFirstPage) {
chunks.push(defaultField.html().substring(i, i + wordsMaxOthersPages));
}
// Execute Array to build the new HTML
for (var key in chunks) {
if (chunks.hasOwnProperty(key)) {
$(this).find('.c_6937').append('<div class="value line_'+key+'" style="font-size: 20px; margin: 0px; line-height: 26px; font-family: Times, serif; font-weight: normal;">' + chunks[key] + '</div>');
console.log(key + " -> " + chunks[key]);
}
}
// Debug
console.log(countLengthOfCommentPage);
The place where things get complicated to understand for me is where I build the array. Yes I know, I use substring Method and this method actually works with caracters.
My question is simple. Is there any way to build the array with a regex function or am I juste missing something very simple?