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


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>


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


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

    $this = $(elem);
    text = $this.html();
    if (\S*:\/\//) == 0) {
        $this.html('Click here');

Here's a demo.

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

                return $(this).text().search(/\S*:\/\//) == 0;
      .text('Click here');