Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Line count shows 1 eventhough input field has no value

I am using below code to calculate words, characters and lines.

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


var textarea = document.getElementById("text");
var result   = document.getElementById("result");

textarea.addEventListener("input", function(){
  var v = wordCount( this.value );
  result.innerHTML = (
      "<br>Characters (no spaces):  "+ v.charactersNoSpaces +
      "<br>Characters (and spaces): "+ v.characters +
      "<br>Words: "+ v.words +
      "<br>Lines: "+ v.lines
  );
}, false);
<textarea id="text"></textarea>
<div id="result"></div>

initially everything is set to zero. When we type it counts well. But when we clear the textbox, Lines shows value as 1 eventhough there is no texts in the textbox. How to make the line count zero?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I realized that splitting val make the length of it 1 even if it's empty:

// val is empty and it length is null
// But after splitting it would be one
val.split(/\r*\n/).length // 1

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.length ? val.split(/\r*\n/).length : val.length
    };
}


var textarea = document.getElementById("text");
var result   = document.getElementById("result");

textarea.addEventListener("input", function(){
  var v = wordCount( this.value );
  result.innerHTML = (
      "<br>Characters (no spaces):  "+ v.charactersNoSpaces +
      "<br>Characters (and spaces): "+ v.characters +
      "<br>Words: "+ v.words +
      "<br>Lines: "+ v.lines
  );
}, false);
<textarea id="text"></textarea>
<div id="result"></div>

about 4 years ago · Juan Pablo Isaza Report

0

When a string is split, if none of the delimiters match, you'll be left with the original string.

const foo = 'foo';
console.log(foo.split(/bar/).length);

The same is true of the empty string - when split on, it'll still give you [''], which has a length..

You might match lines which have at least one character on them instead of splitting, thereby excluding empty lines.

lines: (val.match(/^./gm) || []).length
about 4 years ago · Juan Pablo Isaza Report

0

If you split an empty string, the result is [""] -- an array with one element that's an empty string.

You can check for this case first with a conditional expression.

Also, there's no need to use \r* in the split delimiter, as it doesn't influence the count.

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.length == 0 ? 0 : val.split('\n').length
    };
}


var textarea = document.getElementById("text");
var result   = document.getElementById("result");

textarea.addEventListener("input", function(){
  var v = wordCount( this.value );
  result.innerHTML = (
      "<br>Characters (no spaces):  "+ v.charactersNoSpaces +
      "<br>Characters (and spaces): "+ v.characters +
      "<br>Words: "+ v.words +
      "<br>Lines: "+ v.lines
  );
}, false);
<textarea id="text"></textarea>
<div id="result"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!