Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting long sentences using jQuery

I'd like to highlight long sentences (say, 50 words or greater) contained in an array of paragraph objects on a page, ie $("#content p"). I'm not sure how to tackle this.

I originally tried to highlight all sentences, but ran in trouble when they contained HTML tags (example highlighting code on the net seem to be for individual words only, so they don't take child nodes into account). I'm aware that splitting sentences is difficult; I'd like to use .!? followed either by a space then a capital letter or nothing at all (ie the end of the paragraph).

Thanks in advance for any help/advice.

like image 834
The Wilky Bar Kid Avatar asked Feb 26 '10 12:02

The Wilky Bar Kid


1 Answers

As you've said it's gonna be tricky to get right, given the fact you;re not going to catch them all, I'd stick with something simple like:

var regex = \[^.!?]{50,}[.!?]\;

Getting too clever and you will end up spending more time coding for edge cases than I guess you would reasonably want to.

like image 58
Justin Wignall Avatar answered Oct 23 '22 22:10

Justin Wignall