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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:18:45+00:00 2026-05-26T10:18:45+00:00

Lets say I am typing the word hello into a textarea. How would I,

  • 0

Lets say I am typing the word “hello” into a textarea. How would I, after I had typed hello, select that word I had just typed, and modify it. For example, after I typed the word hello, without hitting the space bar, recognize the word is hello, and before and after it add <b> and </b> around the word. So the ending result would be <b>hello</b> I know textarea’s don’t take HTML, but for the sake of argument. The only way I can think to do this is to run a function each time the user presses a key, and add the content of the textarea to an array, breaking it at the spaces. But that requires spaces, and than having to cross reference the array will take time. Is there anyway to do this easily?

  • 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-05-26T10:18:46+00:00Added an answer on May 26, 2026 at 10:18 am

    You’ll definitely have to trap each keypress if you want it to do the replacement as you type, but you don’t have to test for spaces. The following worked when I tested it in IE:

    $(function() {
       $("#yourtextareaidhere").keyup(function(e) {
          var updatedText = this.value.replace(
                                   /(^|[^>])(hello)($|[^<])/gi, "$1<b>$2</b>$3");
          if (updatedText != this.value)
             this.value = updatedText;
       });
    });
    

    Essentially on every keypress this does a string replace to take all instances of “hello” not already surrounded by “>” and “<” (including where “hello” is at the start or end of the string), and substitute in "<b>hello</b>". If the result of the string replace is different to what was in the field (i.e., some substitution did occur) then the new value is written back to the textarea – I don’t just write it back directly every time because that loses the cursor position which is annoying for the user and I’m too lazy to come up with a better workaround than this simple “if” test. (I justify this on the basis that I’m providing you with a “starting place”, and leaving the rest to you.)

    This approach allows for the fact that the user may not have typed the word “hello” in one go, e.g., if they made a typo and initially had “I say hllo to you” then went back and added the “e” then you (presumably) want to replace the “hello” even though it isn’t at the end. Also I do a global replace so that if the user pastes in “hello hello hello” they’ll all be replaced. Decide for yourself if you want a case-insensitive replacement.

    If there are other words you are looking to replace too you could add them to the same regular expression if they all just need the bold tags, otherwise if each special word has its own required formatting define an array of search regexes and replacement strings and loop through them within the function.

    Note that my regular expressions are a bit rusty so I won’t be at all surprised if there’s a nicer way to do the same thing, but I’ll leave any improvements as an exercise for the reader.

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

Sidebar

Related Questions

Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user
Let's say that I have a jar file @ /usr/local/apps/app1/java/file.jar . I would like
Lets say that me and my friend are trying to work on same software
Lets say I have a class that is doing something like: public class Foo
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have several web sites on my web server, all as applications
Lets say on MySQL database (if it matters).
Lets say I've got two interchangeable pieces of code and I want to figure
Lets say I have an array like this: string [] Filelist = ... I

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.