admin管理员组文章数量:1313086
Hi =) I have some code that I am struggling with. I have a function to replace specific html characters values like a tab or new line, however it doesn't seem to work very well.
var replaceHTMLCharacters = function(text){
text = text.split("");
for(var i = 0; i < text.length; i++){
if(text[i].charCodeAt(0) == 10){
text[i] = "\n";
}
}
text = text.join("");
console.log(text);
return text;
}
This is an example of me trying to remove a new line character and replace it with "\n" so that I can store it in a JSON format. It reaches the point where it says text[i] = "\n"
but then doesn't actually set this character to a "\n"
. Can anyone tell me what I am doing wrong? Thank you.
Hi =) I have some code that I am struggling with. I have a function to replace specific html characters values like a tab or new line, however it doesn't seem to work very well.
var replaceHTMLCharacters = function(text){
text = text.split("");
for(var i = 0; i < text.length; i++){
if(text[i].charCodeAt(0) == 10){
text[i] = "\n";
}
}
text = text.join("");
console.log(text);
return text;
}
This is an example of me trying to remove a new line character and replace it with "\n" so that I can store it in a JSON format. It reaches the point where it says text[i] = "\n"
but then doesn't actually set this character to a "\n"
. Can anyone tell me what I am doing wrong? Thank you.
- Have you try \r\n ? – Ricardo Ortega Magaña Commented Nov 11, 2016 at 0:24
- "\n" is a newline character. You're replacing them with what's already there, leaving the String unchanged. If you want the actual characters \ and n, you need to mask the backslash "\\n" and insert that. – user5734311 Commented Nov 11, 2016 at 0:25
- @ChrisG Ah yes that did the trick for me thank you. Post as answer I will mark as solution – Pixelknight1398 Commented Nov 11, 2016 at 0:35
-
Why do you need to do this to store in JSON?
JSON.stringify()
will do this for you. – Barmar Commented Nov 11, 2016 at 1:14
5 Answers
Reset to default 3"\n" is a newline character. You're replacing them with what's already there, leaving the String unchanged. If you want the actual characters \ and n, you need to mask the backslash \\n
and insert that.
It seems you are replacing "\n" with "\n". To replace '\n' you will need to escape it by adding an additional '\' or '\n'
I think you would be better off using a bit of regex here. Try this code (I also purposely renamed your function to have a bit more describing name):
function breakOnTab (string) {
return string.replace(/\t/g, '\r\n')
}
This regex finds all tab characters \t
and replaces them with a string consisting of escaped carriage return and new line.
You could use something like this with \n and \t ..
You could check also checkout Difference between \n and \r?
var replaceHTMLCharacters = function(text) {
var reg = new RegExp(/(\r\n?|\n|\t)/g);
var ntext = text.replace(reg, "-");
console.log(ntext);
return text;
}
replaceHTMLCharacters(`newLine
Text1`)
replaceHTMLCharacters("Tab text2");
First of all: HTML chars do not exist...
Second: I don't understand why you did text = text.split("");
, you will iterate over an array instead of an String, spending time and memory...
Third: '\n' charCode is 10...
If you want to rewrite text formatted on HTML to plain text, you could do something like:
var replaceHTMLCharacters = function(text){
text = text.replace(/<br\s*\/?>/g,'\n');
console.log(text);
return text;
}
本文标签: htmlHow to replace a new line character in JavascriptStack Overflow
版权声明:本文标题:html - How to replace a new line character in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741925373a2405252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论