js 文本字符串链接转为锚链接

function parseurl(str, mode) {
    if(str.match(/\[code\]((\n|\r|.)*?)\[\/code\]/)||str.match(/\[html\]((\n|\r|.)*?)\[\/html\]/)){
            return str;
    }else{
            str = str.replace(/([^>=\]"'\/]|^)((((https?|ftp):\/\/)|www\.)([\w\-]+\.)*[\w\-\u4e00-\u9fa5]+\.([\.a-zA-Z0-9]+|\u4E2D\u56FD|\u7F51\u7EDC|\u516C\u53F8)((\?|\/|:)+[\w\.\/=\?%\-&~`@':+!]*)+\.(jpg|gif|png|bmp))/ig, mode == 'html' ? '$1<a href="$2" target="_blank">$2</a>': '$1$2');
            str = str.replace(/([^>=\]"'\/@]|^)((((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|ed2k):\/\/)|www\.)([\w\-]+\.)*[:\.@\-\w\u4e00-\u9fa5]+\.([\.a-zA-Z0-9]+|\u4E2D\u56FD|\u7F51\u7EDC|\u516C\u53F8)((\?|\/|:|;)+[\w\.\/=\?%\-&~`@':+!#]*)*)/ig, mode == 'html'?'$1<a href="$2" target="_blank">$2</a>': '$1$2');
            str = str.replace(/([^\w>=\]:"'\.\/]|^)(([\-\.\w]+@[\.\-\w]+(\.\w+)+))/ig, mode == 'html' ? '$1<a href="mailto:$2">$2</a>' : '$1[email]$2[/email]');
            str = str.replace(/href="www/ig,"href=\"http://www");
            return str;
    }
}

你可能感兴趣的:(JavaScript,html)