Answers for "with javascript, can html markup be added in a text string?"

0

with javascript, can html markup be added in a text string?

const text = 'This is a test to see whether nested style spans work properly.'
const styling = [
  {start: 22, end: 54, type: "strong"},
  {start: 30, end: 36, type: "a"},
  {start: 37, end: 48, type: "em"},
  {start: 43, end: 48, type: "a"}
];

const result = [...text].reduce((a, v, i) => {
  styling.filter(s => s.start === i).forEach(s => a += `<${s.type}>`);
  styling.filter(s => s.end === i).forEach(s => a += `</${s.type}>`);
  return a + v;
}, '');

document.body.innerHTML = result;

// EXMAPLE FROM STACKOVERFLOW.COM //
// OUTPUT: 
// This is a test to see <strong>whether <a>nested</a> <em>style <a>spans</em></a> work </strong>properly.
Posted by: Guest on April-12-2021

Code answers related to "with javascript, can html markup be added in a text string?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language