1

I want to change several characters in a string in Node Js, and as it is not possible to use .replaceAll(), I am using .replace() with global variables this way:

function toHTML(content) {
    content = content.replace(/[i]/g, "<i>")
    .replace(/[/i]/g, "</i>")
    .replace(/[b]/g, "<b>")
    .replace(/[/b]/g, "</b>")
    .replace(/[u]/g, "<u>")
    .replace(/[/u]/g, "</u>")
    .replace(/[p]/g, "<p>")
    .replace(/[/p]/g, "</p>")
    .replace(/[cite]/g, "<cite>")
    .replace(/[/cite]/g, "</cite>")
    .replace(/[code]/g, "<code>")
    .replace(/[/code]/g, "</code>");
    
    return content;
  }

If I try with the following string:

This is an example

The output is the following:

Th<<<<<<</code><</code></code></code></code>>it<</code></code></code></code>>>p>u>b><<</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>>>>>s <<<<<<</code><</code></code></code></code>>it<</code></code></code></code>>>p>u>b><<</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>>>>>s an <<</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>>>xam<<<</code><</code></code></code></code>>it<</code></code></code></code>>>p>>l<<</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>><</code><</code></code></code></code>>it<</code></code></code></code>>>>
404
  • 8,022
  • 2
  • 27
  • 47
  • This question is about [tag:javascript]. It is not specific or even related to Node.js in any way. – axiac May 11 '21 at 15:50
  • What is the question? – axiac May 11 '21 at 15:51
  • The output is correct, even if it does not match your expectations. Read about [regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions). – axiac May 11 '21 at 15:53
  • Does this answer your question? [Replace multiple strings with multiple other strings](https://stackoverflow.com/questions/15604140/replace-multiple-strings-with-multiple-other-strings) – Altareos May 11 '21 at 16:07
  • @Altareos Tried but didn't work. Already posted a solution I found. – Egoi Cantero Viqueira May 11 '21 at 19:08
  • [i] <-- that is saying match any i..... that is not matching the string "[" and "]". It would need to be `content.replace(/\[i\]/g, "")` – epascarello May 11 '21 at 19:14
  • `"Hello [i]World[/i] is [b]Funky[/b] [cite][b]C[/b]hicken[/cite]".replace(/\[(\/?(i|b|cite))\]/g,"<$1>")` – epascarello May 11 '21 at 19:27

2 Answers2

2

Or, use (modified) as suggested by @epascarello:

string.replace(/\[(\/?(i|b|p|code|cite))\]/g,"<$1>")
0xLogN
  • 3,289
  • 1
  • 14
  • 35
0

Just found a solution by myself as I couldn't make it work using regular expressions or this solution as suggested.

    function toHTML(content) {

    let converted = content;

    const changes = [
      {from: "[i]", to: "<i>"},
      {from: "[/i]", to: "</i>"},
      {from: "[b]", to: "<b>"},
      {from: "[/b]", to: "</b>"},
      {from: "[u]", to: "</u>"},
      {from: "[/u]", to: "</u>"},
      {from: "[p]", to: "</p>"},
      {from: "[/p]", to: "</p>"},
      {from: "[cite]", to: "</cite>"},
      {from: "[/cite]", to: "</cite>"},
      {from: "[code]", to: "</code>"},
      {from: "[/code]", to: "</code>"},
    ];

    changes.map((item, index) => {
      while(converted.indexOf(item.from) !== -1) {
          converted = converted.replace(item.from, item.to);
        }
    })

    return converted;
  }