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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:24:05+00:00 2026-06-03T04:24:05+00:00

I am writing a jquery plugin that will do a browser-style find-on-page search. I

  • 0

I am writing a jquery plugin that will do a browser-style find-on-page search. I need to improve the search, but don’t want to get into parsing the html quite yet.

At the moment my approach is to take an entire DOM element and all nested elements and simply run a regex find/replace for a given term. In the replace I will simply wrap a span around the matched term and use that span as my anchor to do highlighting, scrolling, etc. It is vital that no characters inside any html tags are matched.

This is as close as I have gotten:

(?<=^|>)([^><].*?)(?=<|$)

It does a very good job of capturing all characters that are not in an html tag, but I’m having trouble figuring out how to insert my search term.

Input: Any html element (this could be quite large, eg <body>)    
Search Term: 1 or more characters    
Replace Txt: <span class='highlight'>$1</span>

UPDATE

The following regex does what I want when I’m testing with http://gskinner.com/RegExr/…

Regex: (?<=^|>)(.*?)(SEARCH_STRING)(?=.*?<|$)
Replacement: $1<span class='highlight'>$2</span>

However I am having some trouble using it in my javascript. With the following code chrome is giving me the error “Invalid regular expression: /(?<=^|>)(.?)(Mary)(?=.?<|$)/: Invalid group”.

var origText = $('#'+opt.targetElements).data('origText');
var regx = new RegExp("(?<=^|>)(.*?)(" + $this.val() + ")(?=.*?<|$)", 'gi');
$('#'+opt.targetElements).each(function() {
   var text = origText.replace(regx, '$1<span class="' + opt.resultClass + '">$2</span>');
   $(this).html(text);
});

It’s breaking on the group (?<=^|>) – is this something clumsy or a difference in the Regex engines?

UPDATE

The reason this regex is breaking on that group is because Javascript does not support regex lookbehinds. For reference & possible solutions: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript.

  • 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-03T04:24:06+00:00Added an answer on June 3, 2026 at 4:24 am

    Just use jQuerys built-in text() method. It will return all the characters in a selected DOM element.

    For the DOM approach (docs for the Node interface): Run over all child nodes of an element. If the child is an element node, run recursively. If it’s a text node, search in the text (node.data) and if you want to highlight/change something, shorten the text of the node until the found position, and insert a highligth-span with the matched text and another text node for the rest of the text.

    Example code (adjusted, origin is here):

    (function iterate_node(node) {
        if (node.nodeType === 3) { // Node.TEXT_NODE
            var text = node.data,
                pos = text.search(/any regular expression/g), //indexOf also applicable
                length = 5; // or whatever you found
            if (pos > -1) {
                node.data = text.substr(0, pos); // split into a part before...
                var rest = document.createTextNode(text.substr(pos+length)); // a part after
                var highlight = document.createElement("span"); // and a part between
                highlight.className = "highlight";
                highlight.appendChild(document.createTextNode(text.substr(pos, length)));
                node.parentNode.insertBefore(rest, node.nextSibling); // insert after
                node.parentNode.insertBefore(highlight, node.nextSibling);
                iterate_node(rest); // maybe there are more matches
            }
        } else if (node.nodeType === 1) { // Node.ELEMENT_NODE
            for (var i = 0; i < node.childNodes.length; i++) {
                iterate_node(node.childNodes[i]); // run recursive on DOM
            }
        }
    })(content); // any dom node
    

    There’s also highlight.js, which might be exactly what you want.

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

Sidebar

Related Questions

I'm writing a jQuery plugin that will need callback functions that are executed, for
I'm writing a simple jQuery plugin that will interface with a JSON object/array. I
I am writing a jQuery plugin that manipulates the value of an input field
I'm currently writing a JQuery plugin that loads colors from a JSON web service
When writing a new jQuery plugin is there a straightforward way of checking that
I am writing a simple GreaseMonkey script that has a jQuery plugin called hoverIntent
I'm writing a jQuery plugin , which similar to this, $(this).each(function(){ $el = $(this).find('.el')
I am writing my first jQuery plugin and I want to be able to
I am writing a jquery plugin that gets a table and allow to change
I'm writing a jQuery plugin that has a couple of callbacks, and am at

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.