当前位置: 动力学知识库 > 问答 > 编程问答 >

\n and \t and getting them to display in a <textarea> using javascript or .net

问题描述:

HI, using C# I am saving formated HTML data in MSSQL such as:

<div>\n\t<p>x</p>\n</div>

I am than populating it into a textarea to display. I understand that I can use the .val() method in jQuery to pull all of the ASCII characters out of the textarea, however, I can't seem to figure out how to get the "\n" and "\t" characters to show up as new lines and tabs.

When I use FireBug to check the html contents of the textarea in question, this is what is displayed:

&lt;div&gt;/\n/\t&lt;p&gt;This is a test Div&lt;/p&gt;/\n&lt;/div&gt;

I don't really care about using jQuery to display the new lines and tabs, I can also use .NET to change the characters, I would just like to know either or both options.

Thanks much!

网友答案:

In .NET you can use String.Replace:

s = s.Replace(@"\n", "\n");
s = s.Replace(@"\t", "\t");

You might want to consider whether you should make this replacement before you insert the data into the database rather than as you fetch it, because you'll probably only be inserting it once but fetching it a lot of times.

On the other hand, if you do it on the client then the work is offloaded from the server. But if they don't have Javascript enabled, it will look wrong.

网友答案:

You should be saving your data better than that... but for now, you can use a simple regex trick:

yourString.replace(/\\n/g, "\n").replace(/\\t/g, "\t");
分享给朋友:
您可能感兴趣的文章:
随机阅读: