Skip to content Skip to sidebar Skip to footer

Splitting Textarea Sentences Into Array And Finding Out Which Sentence Changed On Keyup()

I have a textarea with this content: Since it's a textarea, it

Solution 1:

The easiest way I can see to do this is to split() the str like you mentioned. From my understanding (and not finding any references to this), jQuery doesn't actually have a split() function, however:

var str = $('#text').val();
var sentences = str.split(/[.|!|?]\s/gi);

So as you can see, javascript's split function allows for regular expressions. The regex however isn't perfect (it's getting late, i'll look into getting a better one tomorrow).

Once you have the sentences it's just a matter of counting the length of each string until you get the caret's position. So get length of first sentence, is it bigger of = to the caret position, if not, get length of second string + first string, is that = or bigger. If it is equal or bigger, then that's the sentence you're on and the index your using is the sentence number.

Post a Comment for "Splitting Textarea Sentences Into Array And Finding Out Which Sentence Changed On Keyup()"