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

  • Home
  • SEARCH
  • 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 787283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:10:38+00:00 2026-05-14T21:10:38+00:00

My current project requires locating an array of strings within an element’s text content,

  • 0

My current project requires locating an array of strings within an element’s text content, then wrapping those matching strings in <a> elements using JavaScript (requirements simplified here for clarity). I need to avoid jQuery if at all possible – at least including the full library.

For example, given this block of HTML:

<div>
  <p>This is a paragraph of text used as an example in this Stack Overflow
     question.</p>
</div>

and this array of strings to match:

['paragraph', 'example']

I would need to arrive at this:

<div>
  <p>This is a <a href="http://www.example.com/">paragraph</a> of text used
     as an <a href="http://www.example.com/">example</a> in this Stack
     Overflow question.</p>
</div>

I’ve arrived at a solution to this by using the innerHTML() method and some string manipulation – basically using the offsets (via indexOf()) and lengths of the strings in the array to break the HTML string apart at the appropriate character offsets and insert <a href="http://www.example.com/"> and </a> tags where needed.

However, an additional requirement has me stumped. I’m not allowed to wrap any matched strings in <a> elements if they’re already in one, or if they’re a descendant of a heading element (<h1> to <h6>).

So, given the same array of strings above and this block of HTML (the term matching has to be case-insensitive, by the way):

<div>
  <h1>Example</a>
  <p>This is a <a href="http://www.example.com/">paragraph of text</a> used
     as an example in this Stack Overflow question.</p>
</div>

I would need to disregard both the occurrence of “Example” in the <h1> element, and the “paragraph” in <a href="http://www.example.com/">paragraph of text</a>.

This suggests to me that I have to determine which node each matched string is in, and then traverse its ancestors until I hit <body>, checking to see if I encounter a <a> or <h_> node along the way.

Firstly, does this sound reasonable? Is there a simpler or more obvious approach that I’ve failed to consider? It doesn’t seem like regular expressions or another string-based comparison to find bounding tags would be robust – I’m thinking of issues like self-closing elements, irregularly nested tags, etc. There’s also this…

Secondly, is this possible, and if so, how would I approach it?

  • 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-14T21:10:38+00:00Added an answer on May 14, 2026 at 9:10 pm

    You should probably iterate the dom elements. Here’s a simple recursive dom iterator, you can fill in the rest:

    function iterateDom (node) {
    switch (node.nodeType) {
        case 1: // ELEMENT_NODE
            {
            if (node.tagName != "H1") {
                for (var i=0; i<node.childNodes.length; i++)
                        iterateDom(node.childNodes[i]);
                }
            }
            break;
        case 3: //TEXT_NODE
            {
            // node.nodeValue = node.nodeValue.replace(...);
            break;
        }
    return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My current project requires me to assemble a .zip file containing HTML and text-only
One of the data structures in my current project requires that I store lists
A current jQuery project of mine requires browser resize (width AND height) functionality, some
My current project requires extensive use of bit fields. I found a simple, functional
We are using hibernateTemplate for our dao's within my current project. I have a
I am working on a project that requires localization of all user-facing text in
My current project requires a customized System date, which means a system date and
I've never worked with Database Functions, but my current project requires it. I need
The current project I'm working on requires me to talk with the windows live
I have a spec in my current project that requires us to advise the

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.