0
Input: 'text __city__ text __city__ text __city__'

Output: 'text <i>city</i> text <i>city</i> text <i>city</i>'

The result of the feature is passed into dangerouslySetInnerHTML.

This component is

<div dangerouslySetInnerHTML={{__html: content.replace(/(?:\r\n|\r|\n)/g, '<br />')}}></div>

for this problem (link).

My function is

const transformText = (text) => {
  const tmp = text.split('__');
  var res = '';
  var check = true;
  for (var i = 0; i < tmp.length-1; i++){
    check = !check;
    if (check) {
      res += '<i>'+tmp[i]+'</i>';
    } else if (tmp[i] != undefined){
      res += tmp[i];
    }
  }
  return res;
};

but breaks the rest of the page into IE11 browser.

GiGa
  • 41
  • 7
  • 1
    First of all, if your raw input is not HTML, turning it into HTML and then use `dangerouslySetInnerHTML` is a terrible idea. When transforming your input, your first goal should be to parse it into an array or hash of text/city pairs. Once that is done, you would only need to iterate over that array and render each pair accordingly, using `
  • ` tags for instance, or in a table if that's your preference. On top of it, you would gain granular, individual access to each pair to be able to do whatever you pleased.
  • – Thomas Hennes Nov 27 '17 at 12:18
  • It's not a list, it was just an example. From the API I get a string. – GiGa Nov 27 '17 at 12:22
  • 1
    Well, I'm sorry, but I can only comment on the code you provide. And in that particular case, your approach is terribly wrong, as I outlined in my previous comment. – Thomas Hennes Nov 27 '17 at 12:24