Javascript Word Counter
Say you have a <textarea> and you want to retrieve info about the number of:
function wordCount( val ){
var wom = val.match(/\S+/g);
return {
charactersNoSpaces : val.replace(/\s+/g, '').length,
characters : val.length,
words : wom ? wom.length : 0,
lines : val.split(/\r*\n/).length
};
}
wordCount( someMultilineText ).words; // (Number of words)
Output
charactersNoSpaces : 17,
characters : 17,
words : 17,
lines : 1.
characters : 17,
words : 17,
lines : 1.