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.

Share Improve this question asked Nov 11, 2016 at 0:22 Pixelknight1398Pixelknight1398 5552 gold badges12 silver badges34 bronze badges 4
  • 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
Add a ment  | 

5 Answers 5

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