Home > OS >  Javascript substring replace and output
Javascript substring replace and output

Time:01-30

Hi guys I wonder how to reconstruct string. I need to replace br tag inside string with '\n' new line character. So I'm simply doing it like this:

let a='Some<br>Text'
let b=a.replace(/<br>/gi, '\n');

But when I try to make an output to console this way:

console.log(JSON.stringify(b))

It shows the string like this:

Some\nText

But if I'm doing output this way:

console.log(b)

It returns:

Some
Text

So why? And is it possible to use console.log(JSON.stringify(b)) to show the string in a proper way. I mean like this: Some Text

CodePudding user response:

Because the stringify method converts all the characters to string, so you wont see line breaks as expected. If you want to display your text on the same line, you can just replace (inside your regexp replacer) the newline '\n' with an empty space ' '

CodePudding user response:

just replace br with an empty space instead of \n, since \n means a new line.This is why your second word starts from new line

let a='Some<br>Text'
let b=a.replace(/<br>/gi, " ");

output

Some Text
  •  Tags:  
  • Related