I'm trying to adapt this answer to the case of regexp replacement:
我正在尝试将此答案适用于regexp替换的情况:
<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?
我怎样才能在这里做正则表达式替换?
2 个解决方案
#1
6
The problem appears to be that this variable input
is of the Java type java.lang.String
, which apparently is not the native String type of Rhino. You can avoid this problem by explicitely constructing a JavaScript string:
问题似乎是这个变量输入是Java类型java.lang.String,它显然不是Rhino的本机String类型。您可以通过明确构建JavaScript字符串来避免此问题:
var input = new String(attributes.get("input"));
#2
4
I have found another answer on Alfresco forum.
我在Alfresco论坛上找到了另一个答案。
The problem is, that when the JS code is interpreted, the type of input
can't be determined for sure. It could be java.lang.String
or Javascripts's string
. The proposal from the forum worked for me - just to "cast" input
object to JS string
like this:
问题是,当解释JS代码时,无法确定输入的类型。它可能是java.lang.String或Javascripts的字符串。论坛的提议对我有用 - 只需将输入对象“转换”为JS字符串,如下所示:
var res = (input + "").replace(regex, replace);
Note: I've just concatenated the input with empty string.
注意:我刚刚用空字符串连接输入。
Hope this helps.
希望这可以帮助。
#1
6
The problem appears to be that this variable input
is of the Java type java.lang.String
, which apparently is not the native String type of Rhino. You can avoid this problem by explicitely constructing a JavaScript string:
问题似乎是这个变量输入是Java类型java.lang.String,它显然不是Rhino的本机String类型。您可以通过明确构建JavaScript字符串来避免此问题:
var input = new String(attributes.get("input"));
#2
4
I have found another answer on Alfresco forum.
我在Alfresco论坛上找到了另一个答案。
The problem is, that when the JS code is interpreted, the type of input
can't be determined for sure. It could be java.lang.String
or Javascripts's string
. The proposal from the forum worked for me - just to "cast" input
object to JS string
like this:
问题是,当解释JS代码时,无法确定输入的类型。它可能是java.lang.String或Javascripts的字符串。论坛的提议对我有用 - 只需将输入对象“转换”为JS字符串,如下所示:
var res = (input + "").replace(regex, replace);
Note: I've just concatenated the input with empty string.
注意:我刚刚用空字符串连接输入。
Hope this helps.
希望这可以帮助。