简体   繁体   中英

Detect URL in text without hyperlink and replace normal URL with hyperlink?

I have spent considerable amount of time searching for the solution or trying one, But I did not found one. So my usecase is:

I have a text which can have simple url(with or without http/s) or it can also have hyperlinked url.

What regex should do

It should leave hyperlink url as it is and convert the non hyperlinked url to a hyperlinked URL.

Example Text

I am learning regex from www.codeburst.com and trying regex at <a href="https://regexr.com">Regexr</a> .

Expected Solution

I am learning regex from <a href="www.codeburst.com">www.codeburst.com</a> and trying regex at <a href="https://regexr.com">Regexr</a> .

I have tried

this regex, but it it not working as expected.

/((?!href).((https?:\/\/)||(www\.)|(mailto:)).+)/gi
  1. You probably need a negative lookbehind (?<!href=") which was added to ECMAScript recently, see this answer
  2. be careful with double || which renders tokend behind this useless (hungry match)
  3. also be careful with .+ which matches everything after (including newline with /s regex option)

I would start with

(?<!href=")(((https?:\/\/)|(www\.)|(mailto:))\S+)
(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

The breakout of regex is as follows,

  1. (https?:\\/\\/)? checks for http:\\\\ or https:\\\\ or no http
  2. (www\\.)? checks for www. or no www.

I have checked above regex with following test cases:

  1. href="https://www.regexr.com"
  2. href="http://www.regexr.com"
  3. href="mailto:demo@demo.com"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM