1

I have multiple languages in dotCMS. Language variables may contain single quotes or double quotes. I get access to this variables in my javascript by wrapping them in quotes.

var text = "$text.get('variable_name')";

But if variable contains the same quotes, js code crashes. Regular expretions also dont's not work when variable contains /. I use es5, so template string is not an option.

How can I solve this problem?

  • Possible duplicate of [Escaping quotes in velocity template](https://stackoverflow.com/questions/19579748/escaping-quotes-in-velocity-template) – Ori Marko Aug 19 '17 at 16:04

1 Answers1

2

So ${text.get('variable_name') returns just a java.lang.String which means you have access to all the string.replace() and string.replaceAll() methods. Below is an (untested) example.

var text = "${text.get('variable_name').replace("\"","\\\")}";

dotCMS provides a javascript-ify utility as well, something like:

var text = "$UtilMethods.javascriptify(${text.get('variable_name')})";
wezell
  • 573
  • 3
  • 7