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

296
Views
Add <br> tag using replaceAll() in javascript

I have this string:

export default function App() {
  const string = 'test data,cars,colors,demo';

  return (
    <div className="App">
      <h1>Hello {string.replaceAll(',','<br>')}</h1>
    </div>
  );
}

I expect:

test data<br>cars<br>colors<br>demo

But i get one string without breack inside my string. How to achieve what i expect using replaceAll()?

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

0

In order to display html from string you have to use dangerouslySetInnerHTML

export default function App() {
  const string = 'test data,cars,colors,demo';

  return (
    <div className="App">
      <h1 dangerouslySetInnerHTML={{ __html: `Hello ${string.replaceAll(',','<br>')}`}}></h1>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

Assuming this is a React application you need to use dangerouslySetInnerHTML to add the new HTML to the page.

function Example() {

  const string = 'test data,cars,colors,demo';
  const html = `Hello ${string.replaceAll(',', '<br>')}</h1>`;

  return (
    <div className="App">
      <h1 dangerouslySetInnerHTML={{ __html: html }} />
    </div>
  );

}

// Render it
ReactDOM.render(
  <Example />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Report

0

I don't suggest you dangerouslySetInnerHTML. It introduces both security and performance issues, and even if you are working on learning project, you better avoid such an approach.

The reason why your code does not work, JSX does not magically convert string "<br>" into appropriate element (until there is dangerouslySetInnerHTML, sure).

But JSX can render arrays just fine. It allows us to split initial string into elements: string.split(', ') and then inject JSX's <br /> with the help of .flatMap()(think about it as if .join() could return array with additional elements in between elements of source array).

{
  string.
    split(', ').
    flatMap((el, index) => index ? [<br />, el]: el)
}

This approach is way more powerful than dangerouslySetInnerHTML since instead of simple BR you may use any JSX tree with React custom components, context and event handlers.

Another approach is to replace ', ' with newlines and apply CSS style white-space: pre-wrap(check white-space docs on all values available)

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!