How to replace plain URLs with links?

前端 未结 24 2397
生来不讨喜
生来不讨喜 2020-11-21 05:42

I am using the function below to match URLs inside a given text and replace them for HTML links. The regular expression is working great, but currently I am only replacing t

24条回答
  •  青春惊慌失措
    2020-11-21 06:11

    If you need to show shorter link (only domain), but with same long URL, you can try my modification of Sam Hasler's code version posted above

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

提交回复
热议问题