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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:16:19+00:00 2026-05-19T00:16:19+00:00

here’s a very simple js but i don’t know where to begin. in a

  • 0

here’s a very simple js but i don’t know where to begin.

in a html page, if some text is enclosed by angle brackets, like this:

〈some text〉

i want the text to be colored (but not the brackets).

in normal html, i’d code it like this

〈<span class="booktitle">some text</span>〉

So, my question is, how do i start to write such a js script that search the text and replace it with span tags?

some basic guide on how to would be sufficient. Thanks.
(i know i need to read the whole html, find the match perhaps using regex, then replace the page with the new one. But have no idea how that can be done with js/DOM. Do i need to traverse every element, get their inner text, do possible replacement? A short example would be greatly appreciated.)

  • 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-19T00:16:19+00:00Added an answer on May 19, 2026 at 12:16 am

    It depends partially on how cautious you need to be not to disturb event handlers on the elements you’re traversing. If it’s your page and you’re in control of the handlers, you may not need to worry; if you’re doing a library or bookmarklet or similar, you need to be very careful.

    For example, consider this markup:

    <p>And <a href='foo.html'>the 〈foo〉 is 〈bar〉</a>.</p>
    

    If you did this:

    var p = /* ...get a reference to the `p` element... */;
    p.innerHTML = p.innerHTML.replace(/〈([^〉]*)〉/g, function(whole, c0) {
        return "〈<span class='booktitle'>" + c0 + "</span>〉";
    });
    

    (live example) (the example uses unicode escapes and HTML numeric entities for 〈 and 〉 rather than the literals above, because JSBin doesn’t like them raw, presumably an encoding issue)

    …that would work and be really easy (as you see), but if there were an event handler on the a, it would get blown away (because we’re destroying the a and recreating it). But if your text is uncomplicated and you’re in control of the event handlers on it, that kind of simple solution might be all you need.

    To be (almost) completely minimal-impact will require walking the DOM tree and only processing text nodes. For that, you’d be using Node#childNodes (for walking through the DOM), Node#nodeType (to know what kind of node you’re dealing with), Node#nodeValue (to get the text of a text node), Node#splitText (on the text nodes, to split them in two so you can move one of them into your span), and Node#appendChild (to rehome the text node that you need to put in your span; don’t worry about removing them from their parent, appendChild handles that for you). The above are covered by the DOM specification (v2 here, v3 here; most browsers are somewhere between the two; the links in the text above are to the DOM2 spec).

    You’ll want to be careful about this sort of case:

    <p>The 〈foo <em>and</em> bar〉.</p>
    

    …where the 〈 and the 〉 are in different text nodes (both children of the p, on either side of an em element); there you’ll have to move part of each text node and the whole of the em into your span, most likely.

    Hopefully that’s enough to get you started.

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

Sidebar

Related Questions

Here is a simple timepicker to jQuery UI's datepicker <script type=text/javascript> /* <![CDATA[ */
I have just tried to save a simple *.rtf file with some websites and
Here is an example: I write html code inside of textarea, then I swap
Here is the situation. I am making changes to an application but I do
Here is a scenario: User installs .NET application that you have made. After some
Here is my code sample, let me know if it can be further improved?
Here is my code (Say we have a single button on the page that
Here a simple question : What do you think of code which use try
Here's what I'm trying to do... It's quite simple and obviously there must be
Here is my question. I am having this simple menu. <div id=menu> <ul> <li>

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.