javascript 怎么替换换行符

2024-12-16 18:21:36
推荐回答(4个)
回答1:

如果是前台TEATAREA提交的话,因为IE和FF对换行的处理不同,IE下是 回车+换行 ,FF下是 换行,所以处理方法应该是

var str = '';                           //你的字符串
str = str.replace(/\r\n/g,"
")  
str = str.replace(/\n/g,"
"); 
console.log(str);

回答2:

str=str.replace("\r\n","
");
str=str.replace("\n","
");

回答3:

str=str.replace(/\r\n/,"

\r\n

");

回答4:

var obj="weclome to my blog!";
var newobj=obj.replace("my","our");
alert(newobj);