How to replace plain URLs with links?

前端 未结 24 3102
生来不讨喜
生来不讨喜 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:00

    Identifying URLs is tricky because they are often surrounded by punctuation marks and because users frequently do not use the full form of the URL. Many JavaScript functions exist for replacing URLs with hyperlinks, but I was unable to find one that works as well as the urlize filter in the Python-based web framework Django. I therefore ported Django's urlize function to JavaScript:

    https://github.com/ljosa/urlize.js

    An example:

    urlize('Go to SO (stackoverflow.com) and ask. ', 
           {nofollow: true, autoescape: true})
    => "Go to SO (stackoverflow.com) and ask. <grin>"
    

    The second argument, if true, causes rel="nofollow" to be inserted. The third argument, if true, escapes characters that have special meaning in HTML. See the README file.

提交回复
热议问题