我有一個字符串,其中包含一個字符串(13)作爲linebreak。我如何用例如替換它。 <br>
?我試過mystring.replace("\n","<br>");
但它沒有工作javascript:替換linebreak
在此先感謝。
我有一個字符串,其中包含一個字符串(13)作爲linebreak。我如何用例如替換它。 <br>
?我試過mystring.replace("\n","<br>");
但它沒有工作javascript:替換linebreak
在此先感謝。
"\n"
是chr(10)。我想你想要"\r"
:
mystring.replace("\r", "<br>");
更新時間:更換所有\ r使用正則表達式:
mystring.replace(/\r/g, "<br>");
如果你希望它與Windows,Unix和Mac工作樣式換行符使用此:
mystring.replace(/\r?\n|\r/g, "<br>");
theString.replace(/\n|\r/g, '<br />')
Dupe:http://stackoverflow.com/questions/784313/read-line-break-in-a-string-with-javascript – 2010-02-08 23:11:47
將分行符分配給變量不是更簡單嗎? – 2012-06-04 04:03:49