如何用JavaScript替换\n ?

时间:2021-11-10 21:45:36

I have a textarea where I insert \n when user presses enter. Code from this textarea is sent to a WCF service via jQuery.ajax(). I cannot save \n in DB as it won't show in other applications consuming the service.

我有一个textarea,当用户按enter时,我插入\n。来自这个textarea的代码通过jQuery.ajax()发送到WCF服务。我不能在DB中保存\n,因为它不会显示在其他应用程序中使用服务。

How can i replace \n with <br /> tag?

如何用
标签替换\n ?

solution

解决方案

Well many of you tried and some got right with Javascript Regex with /g (global modifier). At the end i had \n inserted twice, i don't know why, my only guess is that jQuery on keypress event created double \n which i debug.

很多人都试过了,有些人用的是Javascript Regex /g (global modifier)。在结束时,我有两次插入,我不知道为什么,我唯一的猜测是在keypress事件上jQuery创建了double \n,我调试了它。

$('#input').keypress(function (event) {
    if (event.which == '13') {
        inputText = $('#input').val() + '\n';
        $('#input').val(inputText);
    }
});

10 个解决方案

#1


26  

Replace with global scope

替换为全球范围

$('#input').val().replace(/\n/g, "<br />")

or

$('#input').val().replace("\n", "<br />", "g")

#2


11  

it could be done like this:

可以这样做:

$('textarea').val().replace(/\n/g, "<br />");

edit: sorry ... the regular expressions in javascript should not be quoted

编辑:对不起……不应该引用javascript中的正则表达式。

working example

工作示例

#3


6  

Like said in comments and other answer, it's better to do it on server side.

像评论和其他答案一样,最好在服务器端执行。

However if you want to know how to do it on clientside this is one easy fix:

但是,如果你想知道如何在客户端做,这是一个简单的解决方法:

textareaContent.replace(/\\n/g, "<br />");

Where textareaContent is the variable with the data in the textarea.

textareaContent是文本区域中数据的变量。

Edit: Changed so that it replaces globally and not only first match.

编辑:更改,使它在全球范围内替代,而不仅仅是第一场比赛。

#4


2  

If you support PHP you should check this out: http://php.net/manual/en/function.nl2br.php

如果你支持PHP,你应该看看这个:http://php.net/manual/en/function.nl2br.php。

#5


2  

Building on the other answers, this is probably done best by php. Now assuming you don't want to ajax this (which would be pointless and cause unnecessary server load), you should probably use phpjs.org's javascript port of this function:

在其他答案的基础上,这可能是php做得最好的。现在假设您不希望使用ajax(这将是无意义的,并导致不必要的服务器负载),您应该使用phpjs.org的javascript端口:

function nl2br (str, is_xhtml) {
    // Converts newlines to HTML line breaks  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/nl2br    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');    // *     returns 1: 'Kevin\nvan\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '\nOne\nTwo\n\nThree\n'    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';

    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

http://phpjs.org/functions/nl2br:480

http://phpjs.org/functions/nl2br:480

#6


1  

You can use a simple javascript string function.

您可以使用简单的javascript字符串函数。

 string.replace("\n", "<br>")

#7


1  

From within your WCF service can you not just use String.Replace ?

从您的WCF服务中,您可以不只是使用字符串。替换吗?

text = text.Replace("\n","<br />");

#8


1  

var replaced = $('#input').val().replace("\n", "<br/>");

#9


1  

you can use javascript built in replace function with a little help of regex, for example

例如,您可以在regex的帮助下,使用内置的javascript代替函数。

$('#input').val().replace(/\n\r?/g, '<br />')

this code will return all enters replaced with <br>

此代码将返回所有输入,替换为

#10


1  

The following will replace all instances of \n with a <br /> :

以下将用
替换所有\n的实例:

while (message.indexOf("\\n") !== -1) {
   message = message.replace("\\n", "<br />");
}

#1


26  

Replace with global scope

替换为全球范围

$('#input').val().replace(/\n/g, "<br />")

or

$('#input').val().replace("\n", "<br />", "g")

#2


11  

it could be done like this:

可以这样做:

$('textarea').val().replace(/\n/g, "<br />");

edit: sorry ... the regular expressions in javascript should not be quoted

编辑:对不起……不应该引用javascript中的正则表达式。

working example

工作示例

#3


6  

Like said in comments and other answer, it's better to do it on server side.

像评论和其他答案一样,最好在服务器端执行。

However if you want to know how to do it on clientside this is one easy fix:

但是,如果你想知道如何在客户端做,这是一个简单的解决方法:

textareaContent.replace(/\\n/g, "<br />");

Where textareaContent is the variable with the data in the textarea.

textareaContent是文本区域中数据的变量。

Edit: Changed so that it replaces globally and not only first match.

编辑:更改,使它在全球范围内替代,而不仅仅是第一场比赛。

#4


2  

If you support PHP you should check this out: http://php.net/manual/en/function.nl2br.php

如果你支持PHP,你应该看看这个:http://php.net/manual/en/function.nl2br.php。

#5


2  

Building on the other answers, this is probably done best by php. Now assuming you don't want to ajax this (which would be pointless and cause unnecessary server load), you should probably use phpjs.org's javascript port of this function:

在其他答案的基础上,这可能是php做得最好的。现在假设您不希望使用ajax(这将是无意义的,并导致不必要的服务器负载),您应该使用phpjs.org的javascript端口:

function nl2br (str, is_xhtml) {
    // Converts newlines to HTML line breaks  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/nl2br    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');    // *     returns 1: 'Kevin\nvan\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '\nOne\nTwo\n\nThree\n'    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';

    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

http://phpjs.org/functions/nl2br:480

http://phpjs.org/functions/nl2br:480

#6


1  

You can use a simple javascript string function.

您可以使用简单的javascript字符串函数。

 string.replace("\n", "<br>")

#7


1  

From within your WCF service can you not just use String.Replace ?

从您的WCF服务中,您可以不只是使用字符串。替换吗?

text = text.Replace("\n","<br />");

#8


1  

var replaced = $('#input').val().replace("\n", "<br/>");

#9


1  

you can use javascript built in replace function with a little help of regex, for example

例如,您可以在regex的帮助下,使用内置的javascript代替函数。

$('#input').val().replace(/\n\r?/g, '<br />')

this code will return all enters replaced with <br>

此代码将返回所有输入,替换为

#10


1  

The following will replace all instances of \n with a <br /> :

以下将用
替换所有\n的实例:

while (message.indexOf("\\n") !== -1) {
   message = message.replace("\\n", "<br />");
}