简体   繁体   English

将\\ n转换为<br/>

[英]Converting \n to <br/>

I'm having problem converting \\n to a < br / > 我在将\\ n转换为<br />时遇到问题

 function format() { var text = document.getElementById("formatArea").value; text.replace("\\n", "<br/>"); document.getElementById("resultArea").value = text; } 
 <textarea rows="20" cols="80" id="formatArea"> </textarea> <textarea rows="20" cols="80" id="resultArea"> </textarea> <button onclick="format()">Click to create HTML breaks</button> 

Appreciate any help, i'm not very experienced in JS yet. 感谢任何帮助,我对JS的经验还不是很丰富。

You have 2 issues here. 您在这里有2期。

Firstly, replace does not change the original string, it returns a modified string. 首先, replace不会更改原始字符串,它会返回修改后的字符串。 (Documentation link : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace ) (文档链接: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Secondly, if you put a string in the first parameter of replace, it will only replace the first occurence. 其次,如果将字符串放在replace的第一个参数中,它将仅替换第一个出现的字符串。 If you want to replace them all, use a Regular Expression. 如果要全部替换,请使用正则表达式。

So you should have this : 所以你应该有这个:

function format() {
  var text = document.getElementById("formatArea").value;
  text = text.replace(/\n/g, "<br/>");
  document.getElementById("resultArea").value = text;
}

The g ("global") flag on a regular expression is used to match all occurences. 正则表达式上的g (“全局”)标志用于匹配所有出现的事件。

Replace all was something I didn't understand when I started learning JavaScript too! 替换所有内容也是我开始学习JavaScript时不了解的事情!

TL;DR There isn't a replaceAll function in JavaScript. TL; DR JavaScript中没有replaceAll函数。 But replace accepts something called regular expressions . 但是replace接受称为正则表达式的东西。

To replace all foo s with bar s in a string you have to do .replace(/foo/g, "bar") . 要用字符串中的bar替换所有foo ,必须执行.replace(/foo/g, "bar") /foo/g will match all the "foo" s and will replace them with bar. /foo/g将匹配所有"foo" ,并将其替换为bar。 g is called flag and it it means global , meaning you match them all. g称为flag ,它表示global ,表示您将它们全部匹配。

If you want to replace \\n s you have to do: .replace(/\\n/g, "<br/>") , like we did for foo : 如果要替换\\n ,则必须执行: .replace(/\\n/g, "<br/>") ,就像我们对foo所做的那样:

text = text.replace(/\n/g, "<br/>")

Also, you have to assign the new string to the old string, as replace would not modify the original variable value: 另外,您必须新字符串分配给旧字符串,因为replace不会修改原始变量值:

 function format() { var text = document.getElementById("formatArea").value; text = text.replace(/\\n/g, "<br/>"); document.getElementById("resultArea").value = text; } 
 <textarea rows="20" cols="80" id="formatArea"> </textarea> <textarea rows="20" cols="80" id="resultArea"> </textarea> <button onclick="format()">Click to create HTML breaks</button> 

Use /\\n/g for global replace and you have also missed the assignment of the changed text value in text itself, thus the replaced text was not taking effect. 使用/\\n/g进行全局替换,您还错过了text本身中更改后的text值的分配,因此替换后的文本未生效。 You need to assign the replaced value like text = text.replace(/\\n/g, "<br/>"); 您需要分配替换的值,例如text = text.replace(/\\n/g, "<br/>"); :

 function format() { var text = document.getElementById("formatArea").value; text = text.replace(/\\n/g, "<br/>"); document.getElementById("resultArea").value = text; } 
 asd 
 <textarea rows="20" cols="80" id="formatArea"> </textarea> <textarea rows="20" cols="80" id="resultArea"> </textarea> <button onclick="format()">Click to create HTML breaks</button> 

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

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