I'm trying to adapt this answer to the case of regexp replacement:
<scriptdef name="propertyregex" language="javascript">
<attribute name="property"/>
<attribute name="input"/>
<attribute name="regexp"/>
<attribute name="replace"/>
<![CDATA[
var input = attributes.get("input");
var regex = new RegExp(attributes.get("regexp"));
var replace = attributes.get("replace");
var res = input.replace(regex, replace);
project.setProperty(attributes.get("property"), res);
]]>
</scriptdef>
However, executing that code I always get an exception:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException:
The choice of Java constructor replace matching JavaScript argument types
(function,java.lang.String) is ambiguous; candidate constructors are:
class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence)
class java.lang.String replace(char,char)
How can I do regular expression replacement here?
typeof(attributes.get("input"))
you would get"object"
. More simply,String(attributes.get("input"));
would also work. Note that in Nashorn, the JDK 8 JavaScript implementation, nativejava.lang.String
are treated as strings on the JavaScript side. – Marginate