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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:49:16+00:00 2026-05-31T09:49:16+00:00

This Javascript that I found here adds a method parseURL() to the string object

  • 0

This Javascript that I found here adds a method parseURL() to the string object so that if there are any links inside it will surround them with tags so that they become clickable.

String.prototype.parseURL = function() {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
        return url.link(url);
    });
};

First of all, how does this even work? I know a little about regex expressions and can make simple ones, but I don’t even see the word “href” in here!

How can this be modified so that the link opens in another window? i.e. How can I add the target="_blank" property in here?

  • 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-31T09:49:17+00:00Added an answer on May 31, 2026 at 9:49 am

    According to:
    http://www.w3schools.com/jsref/jsref_link.asp
    The String.link() method returns a text “blah” as a HTML link

    <a  href='blah'>blah</a>".
    

    So my guess is you have 2 solutions:

    1/ override the link() definition (not recommended), declaring something like:

    String.prototype.link = function(url) {
        return "<a target='_blank' href='"+url+"'>"+url+"</a>";
    }
    

    2/ define another function and use it instead of link() in the definition of parseURL():

    String.prototype.blankLink = function(url) {
        return "<a target='_blank' href='"+url+"'>"+url+"</a>";
    }
    

    Then your code should become:

    String.prototype.parseURL = function() {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {
            return url.blankLink(url);
        });
    };
    

    To explain a bit, replace() returns the input string where parts matching the regular expression (which in this case expresses the shape of any URL) are replaced by the second argument (the unnamed function that returns url.link()). What the JS reference doesn’t say is that if you give a function – instead of a string – as the second argument of replace(), then replace() will provide the currently matching part as an argument to that unnamed argument-function. Sweet mechanics, assuming this works on any browser 😉

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

Sidebar

Related Questions

I have a javascript that does this (http is your XMLHttpRequest object) var r
Here is the situation. I have some javascript that looks like this: function onSubmit()
im very new to jquery/javascript. So here goes, I found this js code for
My guess is that this javascript just finds the div called divid and then
I have this javascript code below that uses jquery, it is suppoed to be
I have some third-party Javascript that has statements like this: FOO = function() {
I have this Javascript data: [{id:123,type:test},{id:154,type:another}] How would you transform that into something so
I got this javascript code from off the internet. It's a script that changes
How to write this JavaScript code without eval? var typeOfString = eval(typeof + that.modules[modName].varName);
Ok, I have one JavaScript that creates rows in a table like this: function

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.