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.

Example

[this line is empty]
1

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

Example

mydiv.innerHtml.replace(/\s{2,}/g,'').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 *

*

*