1

I have a url like e.g.:

http://www.intereconomia.com/noticias-gaceta/politica/grinan-los-casos-corrupcion-pueden-influir-20120226

I want only in my link text:

http://www.intereconomia.com 

but the href go to:

http://www.intereconomia.com/noticias-gaceta/politica/grinan-los-casos-corrupcion-pueden-influir-20120226

What regex jquery can do this functionality?

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
hyperrjas
  • 10,666
  • 25
  • 99
  • 198
  • Possible dupe? See this question. http://stackoverflow.com/questions/1420881/javascript-jquery-method-to-find-base-url-from-a-string – Burke Holland Feb 28 '12 at 19:54

4 Answers4

6

Instead of using a regex you can try this which is clean and simple.

$('a').each(function(){
    $(this).text(this.protocol + "//" + (this.hostname || this.pathname));
});​

Note: If you want to set it only for set of anchors, then change the selector accordingly but the logic inside remains the same.

Working demo - http://jsfiddle.net/ShankarSangoli/8G7JM/3/

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • 1
    @hyperrjas - Check my edited answer, now added the `protocol` also and support for `pathname` in case `hostname` is not available in some browsers. – ShankarSangoli Feb 28 '12 at 20:38
2

It sounds like you're asking for a simple anchor tag (if you need some specific jQuery/JavaScript action, please elaborate):

<a href="http://www.intereconomia.com/noticias-gaceta/politica/grinan-los-casos-corrupcion-pueden-influir-20120226">
    http://www.intereconomia.com
</a>
James Hill
  • 60,353
  • 20
  • 145
  • 161
  • I generate a link with ruby code `<%= link_to @post.url, @post.url, :target => "_blank", :class => "from_url" %>` – hyperrjas Feb 28 '12 at 19:58
1

You can add text to display for the link:
<a href="http://www.intereconomia.com/noticias-gaceta/politica/grinan-los-casos-corrupcion-pueden-influir-20120226">Click Here</a>

However, I would not suggest making the text http://www.intereconomia.com as that is usually seen as a bad practice to link to an inner-page when the user is expecting to go to http://www.intereconomia.com

Instead use a descriptive link to mask the url.

jzworkman
  • 2,695
  • 14
  • 20
1
//wait for `document.ready` to fire so the DOM elements are available to modify
$(function () {

    //iterate through all the `<a>` elements in the DOM
    $.each($('a'), function () {

        //get the text of the current `<a>` element and then use RegExp to get everything after the `.com/`
        var url   = $(this).text(),
            other = url.replace(/^(http|https):\/\/(.){0,99}(\.com|\.net|\.org)/, '');

        //now change the text of this `<a>` element to not display anything after the `.com/`
        $(this).text(url.replace(other, ''));
    });
});​

​ There is probably a more elegant solution but this should do the trick.

Here is a demo: http://jsfiddle.net/jasper/DssEs/2/

Jasper
  • 75,717
  • 14
  • 151
  • 146
  • Can you delete the end slash **/** `http://www.intereconomia.com` without final slash /. Thank you – hyperrjas Feb 28 '12 at 20:00
  • @hyperrjas sure, just had to remove the `\/` from `(.com\/)`: http://jsfiddle.net/jasper/DssEs/2/ – Jasper Feb 28 '12 at 20:02
  • Thank Jasper, but only works with .com domains. Its possible that works fine with ith any domain?. Thank you – hyperrjas Feb 28 '12 at 20:11
  • @hyperrjas I added `.net`/`.org`, you can add more by changing this piece of RegExp: `(\.com|\.net|\.org)`, just add `|\.blah` to include `.blah` domains. – Jasper Feb 28 '12 at 20:13