Skip to main content



      Home
Home » Eclipse Projects » Advanced Scripting Environment (EASE) » Regexp in String.replaceAll() not working?
Regexp in String.replaceAll() not working? [message #1844804] Sun, 26 September 2021 05:48 Go to next message
Eclipse UserFriend
Hi,
I'm probably overlooking something very trivial but I seem unable to use regexp in search and replace in JS (Rhino) EASE scripts.

No regexp whatsoever seems to ever match, even regexps which work as intended with String.search() and/or very simple (and largely useless) regexps like /abc/g.

I have tried regexps directly in the statement:
txt = txt.replaceAll(/abc/g, 'cba');

defined previously as a literal:
var re = /abc/g;
txt = txt.replaceAll(re, 'cba');

or with the constructor:
var re = new RegExp('abc', 'g');
txt = txt.replaceAll(re, 'cba');

None of these does anything: txt is never altered. I have also tried without the global flag with no difference. This happens both within scripts and in the immediate interpreter. If the same regexp is used with String.search(re) it works as expected.

On the other hand, using String.replace() with a regular expression I get the following error:
org.eclipse.ease.ScriptExecutionException: SyntaxError: The choice of Java method java.lang.String.replace matching JavaScript argument types (function,string) is ambiguous; candidate methods are: 
    class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence)
    class java.lang.String replace(char,char)


What am I doing wrong?
Re: Regexp in String.replaceAll() not working? [message #1845020 is a reply to message #1844804] Sun, 03 October 2021 05:59 Go to previous messageGo to next message
Eclipse UserFriend
You are trying to use a javascript regex as parameter for a Java method. you probably wanna use [1]

HTH
Christian

[1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#replaceAll(java.lang.String,java.lang.String)
Re: Regexp in String.replaceAll() not working? [message #1846994 is a reply to message #1845020] Mon, 11 October 2021 11:55 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for your reply; in fact it helps a lot: by using Java regex things do work.

I am confused, though: the statement is inside a Javascript script, run by the Rhino Javascript engine and the string is obtained through a Javascript DOM-accessing method; what can tell me. in this as well in similar contexts, that this string is not a Javascript object, but rather a Java object with Java method?

Many thanks, mmg

Christian Pontesegger wrote on Sun, 03 October 2021 09:59
You are trying to use a javascript regex as parameter for a Java method. you probably wanna use [1]

HTH
Christian

[1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#replaceAll(java.lang.String,java.lang.String)

Re: Regexp in String.replaceAll() not working? [message #1847010 is a reply to message #1846994] Tue, 12 October 2021 02:38 Go to previous message
Eclipse UserFriend
Hi,

in Rhino you may have as well JavaScript Strings as Java Strings. What type you get depends on the source that creates the string. These 2 String types are a permanent source of confusion and failing scripts. Therefore I recommend to convert Strings to dedicated objects:
var anyString = ....;
var javaScriptString = anyString + "";
var javaString = new java.lang.String(anyString);


HTH
Christian
Previous Topic:chicken and egg: can I create a workspace using an EASE script?
Next Topic:Eclipse 2021-09 and Java 11 does it work?
Goto Forum:
  


Current Time: Tue May 13 14:45:19 EDT 2025

Powered by FUDForum. Page generated in 0.03279 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top