jQuery Trim does not work on Multiple Lines

If you have string where first line is space and there is text in second line, jquery Trim will return empty string.


[this line is empty]

To make it work, first replace all spaces using regex and then apply trim.



Notice regular expression replace(/\s{2,}/g,”)

This will trim all spaces, especially empty lines. Then the trim() funcation will return correct length.

Help was taken from this answer on Stack overflow

Leave a Reply

Your email address will not be published. Required fields are marked *