Javascript String linkify()

Description

Javascript String linkify()


/**// w ww .j av a2  s .  c om
 * The following function converts youtube/vimeo url to links
 */
String.prototype.linkify = function () {
    "use strict";
    var urlPattern = /http:\/\/(?:www\.)?(vimeo|youtube|slideshare|flickr)\.(com|net)\/(?:watch\?v=)?([\s\S]*?)(?:\z|$|&feature=related|&)/mg;
    return this
        .replace(urlPattern, '<a href="$&" class="omebed"></a>');
};

Javascript String linkify()

String.prototype.linkify = function() {
    var replacedText,
        replacePattern1,/*from   www  .ja  v  a 2 s.  c  o m*/
        replacePattern2;

    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;

    replacedText = this.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
    return replacedText;
}

String.prototype.replaceBetween = function(start, end, what) {
    return this.substring(0, start) + what + this.substring(end + 1);
};



PreviousNext

Related