替换JavaScript中的双反斜线-文本区域中的换行符

时间:2021-03-15 18:18:20

I'm having trouble getting my line breaks to show up in a textarea. The data I'm receiving contains double backslashes on line breaks (i.e., \\n ). I've tried using regular expressions to remove one of the backslashes before setting the value of the textarea--not working.

我很难让我的换行符出现在文本框中。我正在接收的数据包含在换行符上的双反斜线。\ \ n)。在设置textarea的值之前,我尝试使用正则表达式删除其中一个反斜杠——不工作。

Here's a re-creation of the problem: http://jsfiddle.net/xkM23/3/

以下是问题的重新创建:http://jsfiddle.net/xkM23/3/

Does anyone know how to get the line breaks to show up properly in that textarea?

有没有人知道如何让换行符在那个文本区域显示?

2 个解决方案

#1


2  

You have to escape \ for JavaScript (and make them double \\). Then it's working as intended: http://jsfiddle.net/xkM23/6/

您必须转义JavaScript(并使它们成为双\)。然后,它按预期工作:http://jsfiddle.net/xkm23/6/。

.replace(/(\\r)|(\\n)/g,"\n")

#2


1  

$('#test_textb').val(
  oTest.data.PRIVATE_CONTACT1.split("\\r\\n").join("\n")
);

#1


2  

You have to escape \ for JavaScript (and make them double \\). Then it's working as intended: http://jsfiddle.net/xkM23/6/

您必须转义JavaScript(并使它们成为双\)。然后,它按预期工作:http://jsfiddle.net/xkm23/6/。

.replace(/(\\r)|(\\n)/g,"\n")

#2


1  

$('#test_textb').val(
  oTest.data.PRIVATE_CONTACT1.split("\\r\\n").join("\n")
);