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

464
Views
Converting array of strings to both JSX Elements and strings in React

I have a tricky one here: I am building a code editor in React that styles different parts of ES6 code. My app works by, converting all the code to one string, then uses Regex to identify where to insert <span>, <strong>, and <br/> tags. I have then taken that long string and used regex with split() to extract all the tags I use into an array. I am now left with this array of strings:

['', '<strong>', 'for', '</strong>', ' (', '<strong>', 'let', '</strong>', ' ', '<span style={{color:"blue"}}>', 'i', '</span>', ' = ', '<span style={{color:"red"}}>', '1', '</span>', '; ', '<span style={{color:"blue"}}>', 'i', '</span>', ' <= ', '<span style={{color:"red"}}>', '10', '</span>', '; ', '<span style={{color:"blue"}}>', 'i', '</span>', '++) {', '<br/>', ',    console.log(', '<span style={{color:"green !important"}}>', '`Pass number ${', '<span style={{color:"blue"}}>', 'i', '</span>', '}`', '</span>', ');', '<br/>', ',}', '<br/>', '']

I need to basically find a way to convert the applicable strings to pure JSX and end up with an array of both strings and JSX.Elements to render out my component. I'm going to take a shower and think, I'm assuming a map of some sort will work??

Appreciate your time and help.

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

0

I ended up going back a step and not converting everything to an Array, and used

 return <div dangerouslySetInnerHTML={{ __html: styledNumbers }}></div>

Works like a charm, for this project. If it was for something public I would likely use this

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!