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.
It should leave hyperlink url as it is and convert the non hyperlinked url to a hyperlinked URL.
I am learning regex from www.codeburst.com and trying regex at <a href="https://regexr.com">Regexr</a>
.
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>
.
this regex, but it it not working as expected.
/((?!href).((https?:\/\/)||(www\.)|(mailto:)).+)/gi
(?<!href=")
which was added to ECMAScript recently, see this answer ||
which renders tokend behind this useless (hungry match) .+
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,
(https?:\\/\\/)?
checks for http:\\\\
or https:\\\\
or no http
(www\\.)?
checks for www.
or no www.
I have checked above regex with following test cases:
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.