最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Calling replace on string throws EvaluatorException - Stack Overflow

programmeradmin5浏览0评论

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.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?

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?

Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked Oct 22, 2014 at 13:52 MvGMvG 60.9k24 gold badges156 silver badges290 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

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:

       var input = new String(attributes.get("input"));

I have found another answer on Alfresco forum.

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:

 var res = (input + "").replace(regex, replace);

Note: I've just concatenated the input with empty string.

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论