我想輸出一個字符串作爲其中的鏈接的HTML。我想讓這些鏈接成爲實際的鏈接。 我的測試字符串= 「https://www.google.com http://yahoo.com www.msn.com www.google.com」 我的代碼:正則表達式來查找文本中的URL,並將它們作爲鏈接
Dim oRegEx As New Regex("((https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)", RegexOptions.IgnoreCase)
Dim matches As MatchCollection = oRegEx.Matches(sTextToConvert)
For Each match As Match In matches
If (match.Value.StartsWith("www.")) Then
sTextToConvert = sTextToConvert.Replace(match.Value, "<a href='http://" & match.Value & "' target=""_blank"">" & match.Value & "</a>")
Else
sTextToConvert = sTextToConvert.Replace(match.Value, "<a href='" & match.Value & "' target=""_blank"">" & match.Value & "</a>")
End If
Next
Return sTextToConvert
我這裏有問題,因爲www.google.com是在串兩次,當我做替換它取代我已經替換字符串「https://www.google.com的一部分。
下面是我得到的替代
<a href='https://<a href='http://www.google.com' target="_blank">www.google.com</a>' target="_blank">https://<a href='http://www.google.com' target="_blank">www.google.com</a></a> <a href='http://yahoo.com' target="_blank">http://yahoo.com</a> <a href='http://www.msn.com' target="_blank">www.msn.com</a> <a href='http://www.google.com' target="_blank">www.google.com</a>
Brad,This is off topic。你如何編輯我的文章?我試圖自己格式化,但沒有爲我工作/ – Mithil 2011-03-10 14:45:52
這是什麼語言? – robert 2011-03-10 15:02:01