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 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

Related Questions

I have a list of URLs, each page is a specific category: http://www.site.com/category-1/page.html http://www.site.com/category-2/page.html
I am trying to write a RegEx rule to find all a href HTML
For example: I know how to match www.domain.com/foo/21 sub foo : Path('/foo') Args(1) {
I'm trying to find URLs in some text, using javascript code. The problem is,
My URL's might not be well formatted, so it could be http://www.google.com/ or www.google.com
My production environment is in a directory off the main url www.example.com/sample . Most
I find that getting Unicode support in my cross-platform apps a real pain in
I find the concept of the programmable, tiling, keyboard-focuessed window manager ion3 very appealing,
I find from reading perldoc perlvar, about a thousand lines in is help for
I find that the .NET event model is such that I'll often be raising

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.