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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:05:54+00:00 2026-05-14T07:05:54+00:00

how do I find URLs (i.e. www.domain.com) within a document, and put those within

  • 0

how do I find URLs (i.e. http://www.domain.com) within a document, and put those within anchors: < a href=”www.domain.com” >http://www.domain.com&lt; /a >

html:

Hey dude, check out this link www.google.com and www.yahoo.com!

javascript:

(function(){var text = document.body.innerHTML;/*do replace regex => text*/})();

output:

Hey dude, check out this link <a href="www.google.com">www.google.com</a> and <a href="www.yahoo.com">www.yahoo.com</a>!
  • 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-14T07:05:55+00:00Added an answer on May 14, 2026 at 7:05 am

    Firstly, www.domain.com isn’t a URL, it’s a hostname, and

    <a href="www.domain.com">
    

    won’t work — it’ll look for a .com file called www.domain relative to the current page.

    It’s not possible to highlight hostnames in the general case because almost anything can be a hostname. You could try to highlight ‘www.something.dot.separated.words’, but it’s not really that reliable and there are many sites that don’t use the www. hostname prefix. I’d try to avoid that.

    /\bhttps?:\/\/[^\s<>"`{}|\^\[\]\\]+/;
    

    This is an very liberal pattern you could use as a starting point for detecting HTTP URLs. Depending on what sort of input you’ve got you may want to narrow down what it allows, and it may be worth detecting trailing characters like . or ! that would be valid parts of the URL but in practice generally aren’t.

    (You could use a | to allow either the URL syntax or the www.hostname syntax, if you like.)

    Anyhow, once you’ve settled on your preferred pattern you’ll need to find that pattern in text nodes on the page. Don’t run the regexp over innerHTML markup. You’ll end up completely ruining the page by trying to mark up every href="http://something" that’s already inside markup. You’ll also destroy any existing JavaScript references, events or form field values when you replace the innerHTML content.

    In general regexp simply cannot process HTML in any reliable way. So take advantage of the fact that the browser has already parsed the HTML into elements and text nodes, and just look at the text nodes. You’ll also want to avoid looking inside <a> elements, since marking up a URL as a link when it’s already in a link is silly (and invalid).

    // Mark up `http://...` text in an element and its descendants as links.
    //
    function addLinks(element) {
        var urlpattern= /\bhttps?:\/\/[^\s<>"`{}|\^\[\]\\]+/g;
        findTextExceptInLinks(element, urlpattern, function(node, match) {
            node.splitText(match.index+match[0].length);
            var a= document.createElement('a');
            a.href= match[0];
            a.appendChild(node.splitText(match.index));
            node.parentNode.insertBefore(a, node.nextSibling);
        });
    }
    
    // Find text in descendents of an element, in reverse document order
    // pattern must be a regexp with global flag
    //
    function findTextExceptInLinks(element, pattern, callback) {
        for (var childi= element.childNodes.length; childi-->0;) {
            var child= element.childNodes[childi];
            if (child.nodeType===Node.ELEMENT_NODE) {
                if (child.tagName.toLowerCase()!=='a')
                    findTextExceptInLinks(child, pattern, callback);
            } else if (child.nodeType===Node.TEXT_NODE) {
                var matches= [];
                var match;
                while (match= pattern.exec(child.data))
                    matches.push(match);
                for (var i= matches.length; i-->0;)
                    callback.call(window, child, matches[i]);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I've set up Textpattern's hak_tinymce plugin on a website I run, and it works
I am trying to trap drag and drop events from the standard Apple address
I am working on a Java project using spring2 and Maven. I have already
I'm looking for some help understanding how to generate pages from a database to
I am trying to use OBJECT to embed content from a remote server into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.