将所有href标记更改为小写

时间:2018-05-16 09:16:42

标签: asp.net vb.net

我有一个包含多个href标签的字符串,例如:

href =“HTTPS://www.example.com”和href =“hTtp://www.example.com”

我需要将其更改为小写,例如:

href =“https://www.example.com”和href =“http://www.example.com”

我需要使用下面的代码来实现它:

Dim strRegex As String = "<?href\s*=\s*[""'].+?[""'][^>]*?"
Dim myRegex As New Regex(strRegex, RegexOptions.IgnoreCase)
For Each myMatch As Match In myRegex.Matches(StringThatContainsHrefTags)
        If myMatch.Success Then
            Dim pattern As String
            pattern = "http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?"
            If Regex.IsMatch(myMatch.ToString(), pattern) Then
            'what do I do here?    
            End If
        End If
    Next

1 个答案:

答案 0 :(得分:0)

如果您不想解析HTML内容,可以这样做。

Dim Input As String = "something <a href=""hTTpS://xxx"">asdf</a> and then <a href=""HttP://xxx"">asdf</a>"
Dim Output As String = Regex.Replace(Input, "<a\s+[^>]*href=""(?<Protocol>https?):", New MatchEvaluator(Function(X) X.Value.ToLower), RegexOptions.IgnoreCase)

这将只处理HTTP / HTTPS部分。您可以将正则表达式扩展到整个URL,并根据需要从代码中执行其他验证。 代码中的快捷方式只需要正则表达式中的一对大括号。否则在X.Groups中进行一些调整(&#34; Protocol&#34;)。将需要值。

相关问题