简体   繁体   English

jQuery textarea用纯文本替换回车

[英]jQuery textarea replace carriage returns with plain text

I have an input with text that user paste in it. 我有一个带有用户粘贴文本的输入。 And I have to find all carriage returns chars and replace it with plain text "\\n" . 而且我必须找到所有回车符char并将其替换为纯文本“ \\ n” "\\n" should be visible in output textarea. “ \\ n”应该在输出文本区域中可见。 Example

I wrote the code, but output area display my "\\n" as HTML tag and make it carriage returns again. 我编写了代码,但是输出区域将我的“ \\ n”显示为HTML标记,并使其再次返回。 But if I use text func I can't carriage returns chars. 但是,如果我使用文本功能,则无法回车字符。 Please help. 请帮忙。

$("#convert").click( function () {
    var txtValue = $('#input').val();
    $('#output').html(txtValue.replace(/\r?\n/g, "\n"));
});

#input and #output are textfields. #input#output是文本字段。

只需转义反斜杠即可:

$('#output').html(txtValue.replace(/\n|\r/g, "\\n"));

you have to use additional slash to put a "\\n" in the string, like this: 您必须使用其他斜杠在字符串中添加“ \\ n”,如下所示:

$("#convert").click( function () {
    var txtValue = $('#input').val().replace(/\n/g, "\\n");
    $('#output').html(txtValue);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM