2013-05-13 143 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) 
相关问题