Adding http: // to all links without a protocol

I am using VB.NET and would like to add http://to all links that do not yet start with http: //, https: //, ftp: // etc.

"I want to add http here <a href=""www.google.com"" target=""_blank"">Google</a>,
but not here <a href=""http://www.google.com"" target=""_blank"">Google</a>."

It was simple when I only had links, but I can’t find a good solution for an entire line containing multiple links. I think RegEx is the way to go, but I don’t even know where to start.

I can find RegEx myself, this is parsing and guesses that I'm having problems with. Can someone give me an example with Regex.Replace () in C # or VB.NET?

Any help appreciated!

+3
source share
4 answers

RFC 1738 Quote:

" : " a "-" z ", (" + "), (". ") (" - "). , URL-, (, " HTTP ", " http ").

! :

/^[a-zA-Z0-9+.-]+:\/\//

href, . , "http://". , . .


EDIT: , ... , , , , . . # HTML-


: , ,

/(<a +href *= *")(.*?)(" *>)/

/^[a-zA-Z0-9+.-]+:\/\//, . ,

$1 + "http://" + $2 + $3

#, .

+1

PHP ( )

$text = preg_replace('/href="(?:(http|ftp|https)\:\/\/)?([^"]*)"/', 'href="http://$1"', $text);
+1

#

 result = new Regex("(href=\")([^(http|https|ftp)])", RegexOptions.IgnoreCase).Replace(input, "href=\"//$2");
+1

, , , contains:

Dim myUrl as string = "someUrlString".ToLower()

If Not myUrl.Contains("http://") AndAlso Not myUrl.Contains("https://") AndAlso Not myUrl.Contains("ftp://") Then

    'Execute your logic to prepend the proper protocol
    myUrl = "http://" & myUrl

End If

Remember that this eliminates a lot of things related to checking which protocol should be used in the appendix and if the URL is relative or not.

Edit: I chose not to specifically offer the RegEx solution, as this is a simple check, and RegEx is a little hard for her (IMO).

0
source

Source: https://habr.com/ru/post/1787667/


All Articles