Find any type of url, and replace & ldquo; Click here & ldquo; Text with regex jquery

advertisements

I need a little script in jQ because I have to change the long urls to a shorten "click here" text. I want to change only the url text not the value of href's attrib like this:

<a href="http://verylongurl.ext/ohshitwhatlongisit/yaythatstoolongforme">http://verylongurl.ext/ohshitwhatlongisit/yaythatstoolongforme</a>

to..

<a href="http://verylongurl.ext/ohshitwhatlongisit/yaythatstoolongforme">click here</a>

The script must work on any possible type of url for example: http: https: ftp: and so on...

tyvm girls and guys!

Regs!


The following searches for links whose text starts with XXXX:// where X is any number of non-whitespace characters and replaces the text with 'Click here':

$('a').each(function(i,elem){
    $this = $(elem);
    text = $this.html();
    if (text.search(/\S*:\/\//) == 0) {
        $this.html('Click here');
    }
});

Here's a demo.


Or, as Felix mentions below, a shorter way to write this would be:

$('a').filter(function(){
                return $(this).text().search(/\S*:\/\//) == 0;
              })
      .text('Click here');