2013-05-13 156 views
1

In my ASP.NET application I have a plain text field. I want to allow the user to be able to enter URL's in the following format:用<a href> tags

[url="http://www.google.com"]Google[/url] 

These would be saved to a database directly as they were entered. On retrieveal however, I'd like to convert the above into the following HTML format to make it active on-screen:

<a href="http://www.google.com">Google</a> 

The reason for this approach is to avoid tampering with the built-in ASP.NET validation routines, which trigger an error when it sees <a in a form input string.

I've seen other examples on StackOverflow where RegEx is used to parse the string, however, I cannot find anything that I can follow where multiple occurrences of [url...] may exist in a single string.

Can anyone please offer me an example of how to parse such a string, say...

Try this funky new search engine: [url="http://www.google.com"]Google[/url] Or this older one from back in the day: [url="http://uk.altavista.com"]AltaVista[/url]

...to convert each occurrence into the desired format? RegEx isn't my strong-point sadly.

Thanks.

+1

幾乎類似的問題,我回答[在正則表達式聊天室](http://chat.stackoverflow.com/transcript/message/9370083#9370083),但在PHP中。如果在ASP中有類似的功能,你可以用'url'和輸出替換'size' ......另外我認爲有一個錯字'Google' I think you meant 'Google'。 – HamZa 2013-05-13 09:44:25

+0

嗨。謝謝是的,有一個錯字糾正。至於RegEx,在VB.NET中它不會返回匹配。不知道是否雙引號會干擾(這是VB.NET中的字符串符號),因此如果有意義的話,您可以使用雙雙引號在字符串中返回單個雙引號? 'Dim r As New Regex(「/ \ [url \ s?= \ s?」「?(。*?)」「?\](。*?)\ [\/url \]/s」)' – EvilDr 2013-05-13 10:06:21

+1

我不熟悉VB.net或ASP,這裏是[在線測試人員](http://i.stack.imgur.com/L2eL5.jpg)的屏幕截圖,你可以加入我們的[正則表達式室]( http://chat.stackoverflow.com/rooms/25767/regex)。 – HamZa 2013-05-13 10:31:28

回答

0

As I understood, you need a BBCode converter for ASP.NET, which you can find here替換自定義標籤。從給定鏈路

例子:

string BBCodeSyntax = "[url={webaddress}]{display}[/url]"; 
string HtmlSyntax = "<a href=\"{webaddress}\">{display}</a>"; 
string Fields = "{webaddress};{display}"; 

string input = "For those who code - [url=http://www.codeproject.com]The CodeProject[/url]. This website contains lots of articles about programming. For open source project hosting from Microsoft, you may have a look at [url=http://www.codeplex.com]Codeplex.com[/url]."; 

string output = BBCode.ConvertToHtml(input, BBCodeSyntax, HtmlSyntax, Fields); 
return output; 
+0

嗨。我明白了,並且還看到了Jeff Atwood的Markdown NuGet軟件包,它模仿了StackOverflow的編輯器。然而,每個人都有他們自己的問題(BBCode使用的iFrames不好),所以我希望儘可能使用本機ASP.NET功能,例如。在BBCodeSyntax中的RegEx – EvilDr 2013-05-13 09:17:32

+0

中,您給出了自定義BBCode的語法,在HTMLSytax中,您提供了自定義HTML語法,您可以在其中編寫iframe,但是在給定示例中,html語法只是簡單鏈接,僅用於顯示href屬性和文本。 – Epsil0neR 2013-05-13 09:46:04

1

繼哈姆扎DzCyber​​DeV的意見,我用正則表達式中RegexHero,發現RegExHero甚至產生了ASP.NET。因此究竟下面的簡單代碼實現了我所需要的結果:

Dim s As String = txt_input.Text.Trim 
Dim strRegex As String = "\[url\s?=\s?""?(.*?)""?\](.*?)\[\/url\]" 
Dim myRegex As New Regex(strRegex) 
Dim strReplace As String = "<a href=""$1"">$2</a>" 
ltl_output.Text = myRegex.Replace(s, strReplace) 
相關問題