(Javascript) Convert plain text links to clickable links

后端 未结 4 1161
离开以前
离开以前 2021-01-06 06:03

Long story short, I have a website made under Wix.com editor, and coding was made possible a few months ago. I have set up a custom comment box, so users can post their comm

4条回答
  •  臣服心动
    2021-01-06 07:10

    It looks like you're replace syntax is wrong.

    Try something like this, I'm pretty sure this will work.

    function linkify(inputText) {
        var replacedText, replacePattern1, replacePattern2, replacePattern3;
    
        //URLs starting with http://, https://, or ftp://
        replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
        replacedText = inputText.replace(replacePattern1, '$1');
    
        //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
        replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
        replacedText = replacedText.replace(replacePattern2, '$1$2');
    
        //Change email addresses to mailto:: links.
        replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
        replacedText = replacedText.replace(replacePattern3, '$1');
    
        return replacedText;
    }
    

    Calling it with:

    $w('#text95').innerHTML = linkify($w('#text95').html);
    

提交回复
热议问题