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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:58:10+00:00 2026-05-28T07:58:10+00:00

I have some text on an HTML page. Need a bookmarklet (no jQuery) that

  • 0

I have some text on an HTML page.
Need a bookmarklet (no jQuery) that will find a segment of the text using regex, then replace it with a link with the text as a parameter

Example before:

aaa bbb ccc ddd

Example after:

aaa <a href="http:www.whatever.com?bbb">bbb</a> ccc ddd

assuming we were looking for “bbb”

  • 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-28T07:58:11+00:00Added an answer on May 28, 2026 at 7:58 am

    This solution will crawl the DOM search for text nodes within the document elements, skipping any elements you want. For example, you probably want to skip <a> tags, as well as <script> tags and others. This way, you won’t replace element nodes or essential page functionality.

    (function(){
        // don't replace text within these tags
        var skipTags = { 'a': 1, 'style': 1, 'script': 1, 'iframe': 1 };
    
        // find text nodes to apply replFn to
        var findKW = function ( el, term, replFn ) {
            var child, tag;
    
            for (var i = el.childNodes.length - 1; i >= 0; i--) {
                child = el.childNodes[i];
                if (child.nodeType == 1) { // ELEMENT_NODE
                    tag = child.nodeName.toLowerCase();
                    if (!(tag in skipTags)) {
                        findKW(child, term, replFn);
                    }
                } else if (child.nodeType == 3) { // TEXT_NODE
                    replaceKW(child, term, replFn);
                }
            }
         };
    
        // replace terms in text according to replFn
        var replaceKW = function ( text, term, replFn ) {
            var match,
                matches = [];
    
            while (match = term.exec(text.data)) {
                matches.push(match);
            }
            for (var i = matches.length - 1; i >= 0; i--) {
                match = matches[i];
    
                // cut out the text node to replace
                text.splitText(match.index);
                text.nextSibling.splitText(match[1].length);
                text.parentNode.replaceChild(replFn(match[1]), text.nextSibling);
            }
        };
    
        var replTerm = prompt('Please enter term to replace');
    
        findKW(
            document.body, 
    
            // using \\b to only replace when the term is the whole word
            // e.g. if term is "bbb" then "aabbbccc" will not match
            new RegExp('\\b(' + replTerm + ')\\b', 'g'),
    
            // your replacement function, change URL accordingly
            function (match) {
              var link = document.createElement('a');
              link.href = 'http://google.com/#q=' + match;
              link.target = '_blank';
              link.innerHTML = match;
              return link;
            }
        );
    }());        
    

    Here it is minimized in bookmarklet form:

    javascript:(function(){var a={a:1,style:1,script:1,iframe:1};var b=function(d,e,f){var g,h;for(var i=d.childNodes.length-1;i>=0;i--){g=d.childNodes[i];if(g.nodeType==1){h=g.nodeName.toLowerCase();if(!(h in a)){b(g,e,f)}}else if(g.nodeType==3){c(g,e,f)}}};var c=function(a,b,c){var d,e=[];while(d=b.exec(a.data)){e.push(d)}for(var f=e.length-1;f>=0;f--){d=e[f];a.splitText(d.index);a.nextSibling.splitText(d[1].length);a.parentNode.replaceChild(c(d[1]),a.nextSibling)}};var d=prompt("Please enter term to replace");b(document.body,new RegExp("\\b("+d+")\\b","g"),function(a){var b=document.createElement("a");b.href="http://google.com/#q="+a;b.target="_blank";b.innerHTML=a;return b})})()
    

    Copy that into a bookmark and try it out on any page! Note: search is case sensitive, but you can add the ‘i’ flag to the RegExp to prevent that.

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

Sidebar

Related Questions

I have used fieldset tag in HTML page, whereby i need some text to
I'm have some that uses jQuery.clone() to get the html of a page and
I have the need to render some html text (not an html page with
I need to enable some disabled fields in my html page that have a
If I for example have <p> some long text </p> on my HTML page,
On extracting some html from a web page, I have some elements containing text
I have a web page with a read-only text box which shows some HTML
I have some text that has HTML hyper-links in it. I want to remove
I have some html files that I want to convert to text. I have
On my html page I have a dropdown list: <select name=somelist> <option value=234234234239393>Some Text</option>

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.