0

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
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
Sachin Jagtap
  • 423
  • 3
  • 11

2 Answers2

1
  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+)
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
0
(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"
Plochie
  • 3,944
  • 1
  • 17
  • 33
  • Your solution works correctly for urls without hyperlink, but if text is 'with hyperlink url ABC', then it gets converted to 'with hyperlink url https://www.example.com">ABC'. For urls inside href, it should keep them as it is. – Sachin Jagtap May 02 '19 at 11:34
  • 1
    You can search all the strings matching with above url, and then find the strings not starting with `href` programmatically. – Plochie May 02 '19 at 12:30