AnsweredAssumed Answered

Ambiguous call to String.replace() on java.lang.Strings

Question asked by jevon on Feb 15, 2012
Latest reply on May 27, 2015 by joviermark
I was trying to use the following code:

var s = escape(args["a"]);

function escape(s) {
  return s.replace(/[\\]/g, "\\\\").replace(/[\"]/g, "\\\"");
}

This is a fairly simple usecase, but I was getting the following error:

The choice of Java constructor replace matching JavaScript argument types (function,string) is ambiguous; candidate constructors are: class java.lang.String replace(char,char); class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence)

This didn't make any sense to me, until I realised that it looks like Alfresco Javascript has TWO Strings: java.lang.Strings and "Javascript" strings.

A workaround is to "cast" it to a "Javascript" string as follows:

function escape(s) {
  return (s + "").replace(/[\\]/g, "\\\\").replace(/[\"]/g, "\\\"");
}

Is this intended behaviour?

Outcomes