Italo Borges
Italo Borges

Reputation: 2554

Split div text without tab space

I would like to split my div text into a array with the text lines. My problem is the space from tabs.

<div class="container">
    Hello,
    Italo Borges
    :)
</div>

When I split the text into lines, I have a lot of blank spaces before each line:

----Hello <- this counts 9 chars
----Italo Borges <- this counts 16 chars

I'm getting the lines with:

var lines = container.text().match(/[^\r\n]+/g);

I need this for animate each chars, but with line break.

Anyone has some idea to resolve this?

Thanks

Upvotes: 0

Views: 200

Answers (2)

EugenSunic
EugenSunic

Reputation: 13703

Loop through every element in the array and trim it:

Example:

var array=[];
var word="";
word=$(".container").text();
array=word.match(/[^\r\n]+/g);
for(var i=0; i< array.length; i++)
{
    alert(array[i].trim().length);
}

You can see that the length is now lower than your's, for each element. You can also insert one other loop inside this loop to get your characters and then animate them afterwards with jquery .animate function.

Upvotes: 1

dacuna
dacuna

Reputation: 1096

You can use the trim function from jquery:

var lines = $.trim(your_string);

For more info check the docs: http://api.jquery.com/jQuery.trim/

Upvotes: 1

Related Questions