1

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

2 Answers 2

2

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 ' '

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.