如何在JavaScript中找到换行符并替换为元素?

时间:2022-12-02 17:09:21

I am trying to find and replace line breaks in text using javascript.

我正在尝试使用javascript查找并替换文本中的换行符。

The following works. It replaces the character a with the character z.

以下作品。它用字符z替换字符a。

var text = "aasdfasdf";text= text.replace(/a/g,"z");alert(text);

The following based on other posts on this and other message boards does not. Basically the javascript does not fire:

以下基于此和其他留言板上的其他帖子没有。基本上javascript不会启动:

var text = "aasdfasdf";text= text.replace(/\n/g,"z");alert(text);

...Here is one of many posts that suggests it should work.

…下面是许多建议它应该发挥作用的文章之一。

JavaScript: How to add line breaks to an HTML textarea?

JavaScript:如何向HTML文本区域添加换行符?

and by the way following does not work for me in Firefox either:

顺便说一下,在火狐浏览器中,我也不喜欢下面的东西:

text = text.replace(/\n\r?/g, '<br />'); ortext = text.replace("\n", '<br />'); 

Note: I realize there are no line breaks in the text variable..I am just using a simple string for testing purposes.

注意:我知道文本变量中没有换行符。我只是为了测试目的而使用一个简单的字符串。

Can anyone see what could be wrong or show me a way to do this that actually works.

谁能看出哪里出了问题,或者告诉我一种可行的方法吗?

Thanks.

谢谢。

4 个解决方案

#1


8  

I'd cover my bets by handling \r\n (the sequence), and then handling \r and \n through a character class, like this:

我通过处理\r\n(序列),然后通过字符类处理\r和\n,如下所示:

text = text.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');

The first replace turns the sequence \r\n into <br />. The second replace replaces any \r or \n characters found on their own with the string.

第一个替换将序列\r\n转换为
。第二个替换用字符串替换自己找到的所有\r或\n字符。

More on regular expressions in JavaScript here.

更多关于JavaScript正则表达式的内容。

#2


1  

ECMAScript normalizes line breaks in strings to "\n\r" and the DOM normalizes line breaks in strings to "\n". Both of those OS agnostic which these formats:

ECMAScript将字符串中的断行归为“\n\r”,DOM将字符串中的断行归为“\n”。这两种格式都是不可知的

  • Windows - CRLF
  • Windows——CRLF
  • Unix - LF
  • Unix -低频
  • Old Mac - CR
  • 旧的Mac - CR

The right way to accomplish this task depends on how you are receiving the string and how you are writing it out.

完成此任务的正确方法取决于您如何接收字符串以及如何将其写入。

#3


0  

To handle windows new line characters try

要处理windows新行字符,请尝试

text = text.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');

#4


0  

Another solution for both types of line endings

这两种类型的行结束的另一个解决方案

str.replace(new RegExp('\r?\n','g'), '<br />');

#1


8  

I'd cover my bets by handling \r\n (the sequence), and then handling \r and \n through a character class, like this:

我通过处理\r\n(序列),然后通过字符类处理\r和\n,如下所示:

text = text.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');

The first replace turns the sequence \r\n into <br />. The second replace replaces any \r or \n characters found on their own with the string.

第一个替换将序列\r\n转换为
。第二个替换用字符串替换自己找到的所有\r或\n字符。

More on regular expressions in JavaScript here.

更多关于JavaScript正则表达式的内容。

#2


1  

ECMAScript normalizes line breaks in strings to "\n\r" and the DOM normalizes line breaks in strings to "\n". Both of those OS agnostic which these formats:

ECMAScript将字符串中的断行归为“\n\r”,DOM将字符串中的断行归为“\n”。这两种格式都是不可知的

  • Windows - CRLF
  • Windows——CRLF
  • Unix - LF
  • Unix -低频
  • Old Mac - CR
  • 旧的Mac - CR

The right way to accomplish this task depends on how you are receiving the string and how you are writing it out.

完成此任务的正确方法取决于您如何接收字符串以及如何将其写入。

#3


0  

To handle windows new line characters try

要处理windows新行字符,请尝试

text = text.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');

#4


0  

Another solution for both types of line endings

这两种类型的行结束的另一个解决方案

str.replace(new RegExp('\r?\n','g'), '<br />');