0

I am using wiris editor within TinyMCE editor in one of my projects.

For some reason I want to replace all charCodes used in content with just character, I know that I can do it for every character 1 by 1 like:

str.replace("&", "&");
str.replace(">", ">");

but content contains hundreds of symbols and I don't want to replace all characters manually. Is there a way to filter the string and replace every charCode with its respective character? e.g. replace <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&#38;</mo></math> with <math xmlns="http://www.w3.org/1998/Math/MathML"><mo>&</mo></math>

  • The linked question's answers show how to do this for HTML entities *generally* (and so is a good dupetarget), but if you **only** need to do specifically decimal character entities and not for (say) `—` or `&`, you can more directly do `str = str.replace(/\(\d+)/g, (_, c0) => String.fromCharCode(+c0));` That uses a regular expression to capture the decimal code, converts it to a number, and gets the character for that number. – T.J. Crowder Mar 16 '22 at 16:22
  • @T.J.Crowder yes I was looking for exactly this. thank you so much man you have saved me. I've been searching this solution for a while. Only thing missing in above code was `;` after `(\d+)`... many many thanks – Noman Nawaz Mar 16 '22 at 16:43

0 Answers0