开发者

VB.NET: Regular Expression needed to replace return character with another string

I'm trying to replace all the carriage return characters in a string obtained from a multi line text box in a Windows Form with the string ", <BR>" so that when I use the string in some HTML it displays correctly.

Function Blah(ByVal strInput As String) As String
  Dim rexCR As开发者_如何学Python Object
  rexCR = CreateObject("VBScript.RegExp")
  rexCR.Pattern = "\r"
  rexCR.Global = True
  Blah = rexCR.Replace(strInput, ",<BR>")
End Function

Tried searching for any of the following characters and still no luck:

\r|\n|\r\c|\cM|\x0d

Problem seems to be the function/expression is not detecting any carriage returns in the text and I have no idea why? I know the function works as I can put a different expression in there as a test and it's OK

Any ideas?


How about normal strInput.Replace(vbCrLf,",<BR>") without regex?


Others have already provided good solutions to your problem. As a general remark, I would like to explain that VB.NET does not have string escape sequences (\n, \r, \t, ...) like C#. String literals in VB.NET are similar to verbatim string literals in C# -- the only character that can be escaped is the double-quote (by doubling it).

Instead, you have to use the VB constants (e.g. vbCrLf, vbTab) or .net constants (e.g. Environment.NewLine) and string concatenation ("Hello World" & vbCrLf instead of `"Hello World\n").


How about:

Function Blah(ByVal strInput As String) As String
  return strInput.Replace(Environment.NewLine, "<br />")
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜