2013-05-13 3 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에 비슷한 기능이 있다면'size'를'url'과 출력으로 바꿀 수 있습니다 ... 또한 나는 오타'Google' I think you meant 'Google'이 있다고 생각합니다. – HamZa

+0

안녕하세요. 감사합니다. 오타가있었습니다. RegEx의 경우 VB.NET에서는 일치 항목을 반환하지 않습니다. 확실하지 않으면 이중 따옴표가 VB.NET에서 문자열 기호로 간섭하는지 확실하지 않으므로 이중 따옴표를 사용하여 문자열에 단일 큰 따옴표를 반환하십시오. 'Dim \ r 새 정규식으로 "/ \ [url \ s? = \ s?" "? (. *?)"? \] (. *?) \ [\ url \]/s ")' – EvilDr

+1

VB.net 또는 ASP에 익숙하지 않아서 [온라인 테스터] (http://i.stack.imgur.com/L2eL5.jpg)의 스크린 샷을 볼 수 있습니다. [정규식 룸]에 참여할 수도 있습니다 (http://chat.stackoverflow.com/rooms/25767/regex). – HamZa

답변

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 기능을 사용하여이 작업을 수행하려고합니다. RegEx – EvilDr

+0

BBCodeSyntax에서 사용자 정의 BBCode의 구문을 제공합니다. HTMLSytax에서는 원할 경우 iframe을 작성할 수있는 사용자 정의 HTML 구문을 제공하지만, html 구문은 href 속성 및 표시 할 텍스트와 관련된 간단한 링크입니다. – Epsil0neR

1

함자 DzCyberDeV의 의견에 따라 나는 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)