-1

is there a special function for this replacement in Javascript ? (replaceAll)

PARAMS+= "&Ueberschrift=" + ueberschrift.replaceAll("&gt;",">").replaceAll("&lt;","<");
         PARAMS+= "&TextBaustein=" + textBaustein.replaceAll("&gt;",">").replaceAll("&lt;","<"); 
         PARAMS+= "&Beurteilung=" + beurteilung.replaceAll("&gt;",">").replaceAll("&lt;","<"); 
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Max Lindner
  • 171
  • 1
  • 1
  • 7
  • I think you are solving the wrong problem. Why do those variables contain HTML entities in the first place? That shouldn't happen. There is a problem reading or creating them. – RoToRa Feb 01 '23 at 13:59

1 Answers1

-1

edit: there is a replaceAll() method in JS, my bad !

anyhow, you can use the replace() method and use a regular expression to replace all occurrences of a string, taken from your provided example you could do something like this:

PARAMS += "&Ueberschrift=" + ueberschrift.replace(/\>/g, "&gt;").replace(/\</g, "&lt;"); PARAMS += "&TextBaustein=" + textBaustein.replace(/\>/g, "&gt;").replace(/\</g, "&lt;"); PARAMS += "&Beurteilung=" + beurteilung.replace(/\>/g, "&gt;").replace(/\</g, "&lt;");

To elaborate: 'g' flag indicates that the replacement should occur for all matches (not just the first one) > and < characters are escaped to match the actual > and < characters. '&gt' and '&lt' (HTML escape codes for '>' and '<')