如何在 JavaScript 中用 <br /> 替换 \n?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6331895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:16:43  来源:igfitidea点击:

How to replace \n with <br /> in JavaScript?

javascriptjqueryregex

提问by eugeneK

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

我有一个文本区域,\n当用户按下回车键时我会在其中插入。来自此文本区域的代码通过 发送到 WCF 服务jQuery.ajax()。我无法保存\n在数据库中,因为它不会显示在使用该服务的其他应用程序中。

How can i replace \nwith <br />tag?

我怎样才能\n<br />标签替换?

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.

好吧,你们中的许多人都尝试过,有些人使用带有 /g(全局修饰符)的 Javascript 正则表达式得到了正确的结果。最后我 \n 插入了两次,我不知道为什么,我唯一的猜测是 jQuery 在 keypress 事件上创建了我调试的双 \n。

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

回答by BrunoLM

Replace with global scope

替换为全局范围

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

or

或者

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

回答by Teneff

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

工作示例

回答by rzetterberg

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 textareaContentis the variable with the data in the textarea.

textareaContent文本区域中包含数据的变量在哪里。

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

编辑:更改为全局替换,而不仅仅是第一次匹配。

回答by Allman

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

回答by Senad Me?kin

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

例如,您可以在正则表达式的帮助下使用内置的 javascript 替换函数

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

this code will return all enters replaced with <br>

此代码将返回所有输入替换为 <br>

回答by Tomas

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 Tó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, '' + breakTag + '');
}

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

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

回答by parliament

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

以下将\n用 a替换所有实例<br />

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

回答by Qtax

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

回答by Wicked Coder

You can use a simple javascript string function.

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

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

回答by Richard Friend

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

在您的 WCF 服务中,您不能只使用String.Replace?

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