Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8100561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:45:48+00:00 2026-06-05T22:45:48+00:00

I have a textarea with this content: <textarea id=text> Hello! Who am I? This

  • 0

I have a textarea with this content:

<textarea id="text">
Hello! Who am I? This is the third sentence. This, is another sentence.
</textarea>

Since it’s a textarea, it can be focused. I’m trying to find a way to:

  1. Separate all sentences inside the textarea into a javascript array. I tried a $(‘#text’).val().split() approach, but there is more than just one sentence separator in my example.

  2. I also need to know in which sentence the cursor is currently positioned on.

Why, you may ask? I need to process (with a paid API that charges per character) sentences when the user changes their content. If I only make a small change in the 2nd sentence, why should the 1st, 3rd and 4th sentences be submitted? It is of course much less expensive to only submit the 2nd one.

As I previously said, the .split() approach would be perfect if people only used dots to separate sentences, but there are other characters that should also be considered (!?.) — about the second point, i found a function to find de caret position:

function doGetCaretPosition (ctrl) {
    var CaretPos = 0;
    if (document.selection) {
        ctrl.focus ();
        var Sel = document.selection.createRange ();
        Sel.moveStart ('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    } else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
        CaretPos = ctrl.selectionStart;
    }
    return (CaretPos);
}

The question is, how does the position help me find the sentence number? All ideas are welcome!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:45:49+00:00Added an answer on June 5, 2026 at 10:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this: <textarea name=content></textarea> and in javascript code in the same aspx file
I have a <textarea> in my wp theme settings. How can I convert this
I have this simple textarea. When it's not selected, I want it to look
I have this following setup, a textarea named with some data in it that
I have this class for my UI public class MyFrame extends JFrame{ JTextArea textArea;
I'm have a very annoying issue with a textarea when trying to retrieve its
i have this problem: i have a form with textarea field, and on a
I have the following Javascript to copy the user's textarea input in this iPhone
We have a multiline textarea in Internet Explorer. If we check it's content after
I have a textarea inside a form (form action=post) and a link under this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.