问题描述
大家好,
我有一个textarea控件。我把它的价值放在一封html电子邮件中。
问题是新行被忽略了。我想取一个
控制值并用html< br>
标记替换任何换行回车。我尝试了以下功能,但它不起作用。有没有人有什么想法如何实现这个?提前致谢。 ~CK
string getComments()
{
string s = tbxComments.Text;
正则表达式r =新正则表达式(" / \ n / g");
s = r.Replace(s," < br />");
返回s;
}
Hi all,
I have a textarea control. I am putting it''s value in an html email. The
problem is that the new lines are being ignored. I want to take the
controls value and replace any newline carriage returns, with an html <br>
tag. I tried the following function but it doesn''t work. Does anyone have
any ideas how to accomplish this? Thanks in advance. ~ CK
string getComments()
{
string s = tbxComments.Text;
Regex r = new Regex("/\n/g");
s = r.Replace(s, "<br/>");
return s;
}
推荐答案
首先要做的事情(IMO)是在不必要的情况下摆脱使用常规的
表达式。我不是一个正则表达式的专家,所以我不会告诉你,如果上面没有找到它是什么
你会期望它 - 但我*知道以下会做什么
你会期望它:
string replace = tbxComments.Text.Replace(" ; \ n","< br />");
现在,这只涉及换行,而不是回车 - 但是你
之后可以随时删除所有回车。
-
Jon Skeet - < sk *** @ pobox.com>
博客:
如果回复小组,请不要给我发邮件
The first thing to do (IMO) is to get rid of the use of regular
expressions when they''re unnecessary. I''m not a regex expert, so I
couldn''t tell you without looking it up whether the above does what
you''d expect it to - but I *do* know that the following will do what
you''d expect it to:
string replaced = tbxComments.Text.Replace ("\n", "<br />");
Now, that only deals with line feeds, not carriage returns - but you
could always remove all carriage returns afterwards.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
这篇关于用< br>替换/ n请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!