How to replace plain URLs with links?

前端 未结 24 2291
生来不讨喜
生来不讨喜 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 06:00

    Reg Ex: /(\b((https?|ftp|file):\/\/|(www))[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]*)/ig

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

    Below are some tested string:

    1. Find me on to www.google.com
    2. www
    3. Find me on to www.http://www.com
    4. Follow me on : http://www.nishantwork.wordpress.com
    5. http://www.nishantwork.wordpress.com
    6. Follow me on : http://www.nishantwork.wordpress.com
    7. https://stackoverflow.com/users/430803/nishant

    Note: If you don't want to pass www as valid one just use below reg ex: /(\b((https?|ftp|file):\/\/|(www))[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig

提交回复
热议问题