Loading:


Zamiana adresów http www url na linki - JavaScript

Funkcja pobiera ciąg znaków i zamienia format http:// na link do strony. funkcje można dowolnie zmodyfikować.


function replaceURLWithHTMLLinks(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig|>;
return text.replace(exp,"<a href='$1'>$1</a>");
}

 

Użycie:

tekst = "Test http://www.wp.pl ciąg dalszy";
var nowyTekst=replaceURLWithHTMLLinks(tekst);



Napisz Artyku³

Listing

niema




Dodano przez: igor
Ranga: Administrator serwisu Punktów: 28716
Komentarze użytkowników
Troche bardziej rozbudowana funckja
  • Zobacz kod dodany do tego wpisu
    • function Linkify(inputText) {
          //URLs starting with http://, https://, or ftp://
          var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim|>;
          var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

          //URLs starting with www. (without // before it, or it'd re-link the ones done above)
          var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
          var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

          //Change email addresses to mailto:: links
          var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
          var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

          return replacedText
      }
autor: igor | 6120 | 2010-09-08 11:34:48


    • Tre¶æ komentarza
      Kod do komentarza (opcjonalnie)
      PHP JavaScript MySQL Smarty SQL HTML CSS ActionScript
      Autor
      Token
      token

       

       








funkcje.net
Wszelkie prawa zastrzeżone©. | Funkcje.net 2008-2024 v.1.5 | design: diviXdesign & rainbowcolors