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 7855155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:18:04+00:00 2026-06-02T20:18:04+00:00

I’m trying to generate the complete sentence in which a word was highlighted using

  • 0

I’m trying to generate the complete sentence in which a word was highlighted using javascript.

window.getSelection 

get me the highlighted word. I have been searching for examples of javascript ranges but still don’t fully understand how to loop by moving the selection backwards till a punctuation is found and then forwards till a punctuation is found.

example:

Social media has a fraught relationship with neurosis. Obsessive
people are essential to sites like Facebook and Twitter. They add
energy and buzz. Their identities get tied up with their avatars, and
that in itself makes the sites seem important. They provide much of
the content. A study published last fall reported that twenty thousand
users on Twitter provide half of what’s read there. But obsessives are
dangerous, too. They can make the site seem creepy.

if I selected the word “Facebook” above, my final string should be “Obsessive people are essential to sites like Facebook and Twitter.”


Update 1:

the code mentioned below works but stops if there are html tags.

Example:

If you delve into the Commerce Department report, you can find some even more disappointing
numbers. Setting aside the production of inventories—goods that
companies add to their stockpiles in anticipation of selling them
later—the economy grew by just 1.6 per cent in the quarter.
Capital investment, which should be surging at this point in the
recovery, hardly rose at all. And personal disposable income—the
amount of money people have to spend after paying taxes—expanded
by just 0.4 per cent.

by selecting “find” above, the output sentence is “you can find some even more disappointing numbers” instead of “If you delve into the Commerce Department report, you can find some even more disappointing numbers”

how would I remove tags while matching before and after?

  • 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-02T20:18:06+00:00Added an answer on June 2, 2026 at 8:18 pm

    window.getSelection returns a Selection object, from which you can obtain a Range, which, in turn, provides you with start and end offsets. The rest is obvious:

    rng = window.getSelection().getRangeAt(0)
    start = rng.startOffset
    end = rng.endOffset
    text = rng.startContainer.nodeValue
    
    before = text.substr(0, start)
    sel = text.substr(start, end - start)
    after = text.substr(end)
    
    sentence = before.match(/[^.!?]*$/)[0] +  sel + after.match(/^[^.!?]*/)[0]
    

    This works in FF and Chrome, IIRC, things look different in MSIE. See http://msdn.microsoft.com/en-us/library/ie/ms535869%28v=vs.85%29.aspx for more info.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has

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.