Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Javascript substring replace and output

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!