Replace text in textarea using Javascript
<p>I need to replace all the matches of a regular expression till the caret position in a textarea using Javascript.
For example, if the text in the textarea is: "6 students carry 2 books to 5 classes" and the cursor 
is placed on books and the regular expression is /\d/, the numbers 6 and 2 should be replaced by, say, 4. 
I know the replace function and I know how to get the caret position, but how do I solve this problem? 
Thanks for any help in advance! </p>
