0

Word counter in JavaScript

JavaScript
const 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
    };
}

const counter = wordCount("This is example text");

console.log(counter.words); // 4
console.log(counter.lines); // 1
console.log(counter.characters); // 20
console.log(counter.charactersNoSpaces); // 17
RUN
add comment
Suggest a different solution or add another example