On Nov 5, 2:54 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Today I struggeled with replace function
>
> I have sample script that works in browser and replaces class=" with
> class=\"
>
> A=function(){};
>
> A.prototype.re1=/"/g;
> A.prototype.doRep=function(s){
> return s.replace(this.re1,"\\\"");
>
> }
>
> console.log(new A().doRep("class=\""));
>
> I took the same script and ported to java:
>
> public static void main(String[] args) {
> Context cx = Context.enter();
> ScriptableObject scope = cx.initStandardObjects();
>
> StringBuilder sb = new StringBuilder();
> sb.append("A=function(){}; \n");
> sb.append("A.prototype.re1=/\"/g; \n");
> sb.append("A.prototype.doRep=function(s){\n");
> sb.append("return s.replace(this.re1,\"\\\"\"); \n");
> sb.append("}; \n");
>
> sb.append("java.lang.System.out.println(").append("new
> A().doRep(\"class=\\\"\")").append(");");
>
> cx.evaluateString(scope, sb.toString(), "<cms>", 1, null);
> Context.exit();
> }
>
> In java the same script does *not* work. Does anyone know the reason
> or can assure that this should not be like that and is a bug?
I didn't run your code, but I do know that Rhino treats "class" as a
reserved word, even though it doesn't currently do anything. So you
probably have to quote it here. I think you can make Rhino much more
lenient w/r/t keywords via a switch. I saw an earlier message on this
list about that a few weeks ago.
A
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino