3

I'm trying to count all the empty lines inside a string. My current function kind of works:

function count_empty(text) {
    var regex = /\r\n?|\n/g;
    var lines = text.split(regex);
    var sep = [];
    $.each(lines, function(k, val) {
        if(!val) {
            //sentence
            sep.push(val);
        }
    });
    return sep.length;
}

...but I really think it could be achieved with a far better approach with something like this:

function count_empty(text) {
    return (text.match(/\r\n?|\n/g) || []).length;
}

Of course, the regex in the 2nd alternative should be retouched to actually accomplish the requirements. So, the question is: What regex should I use in the second approach to retrieve the blank lines only?

One consideration: If a line contains whitespaces only, it will be treated as an empty line.

This textarea's string should return 3 with the function.

<textarea id="test">first line

third line


</textarea>

Thanks!

2
  • Should this <textarea id="test"></textarea> be a count of 1 or 0, i.e. must a line be terminated by \r?\n? When you say spaces do you mean exactly that or any whitespaces? Commented Feb 1, 2015 at 8:29
  • @Xotic750 hi! the count should be 0 in that case, and i meant whitespaces. Commented Feb 1, 2015 at 8:31

3 Answers 3

5

If you enable multi-line mode, m, for your regular expressions, you can match empty lines, ^$, and count how many matches have been found. Note that [ \t]* will match zero or more spaces or tabs.

function count_lines(text){
    return text ? (text.match(/^[ \t]*$/gm) || []).length : 0;
}

This should be really fast since no post-processing after the regular expression is necessary.

See Regex101 for a demo with annotations.

Sign up to request clarification or add additional context in comments.

11 Comments

Hi! If I put some extra empty lines at the end of the string, they are not being counted with this regex.
@andufo, thanks. It looks like the \s* quantifier was consuming adjacent newlines. I have updated the regex as necessary.
Just tried lots of scenarios with your regex, it seems it is working as expected. Thanks!
Treats <textarea id="test"></textarea> as 1 and only handles spaces and tabs and not all whitespaces.
@Xotic750 you're right, forgot to test that case when the textarea is empty.
|
1

What about this?

function count_empty(text) {
    return (text.match(/(^[ \t]*(\n|$))/gm) || []).length;
}

Fiddle

2 Comments

It isn't counting the last enter (if an empty line is added at the end)
true, I've updated just in case you want to try some other approaches. Thanks :)
0

You can just do this

function count_lines(txt){
   return (txt.match(/\n(?=\n/g)) || []).length;
}

What the above does is matches \n which is followed by another \n, and then returning the length of matches.

DEMO

6 Comments

Just two things: 1) i added retouched the return value in case there are no matches (to avoid js errors): return (text.match(/\n(?=\n)/g) || []).length; and 2) for some reason, if I leave one empty line at the end of the string, it is not being counted.
@andufo it does count. Doesn't it? Try adding an extra line in the demo. It should show 4
A parenthesis is missing around somewhere after your last update :s
@andufo yup. A typo :p
Thanks for the help, had to go with @GioBorje's solution, it worked in all scenarios.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.