Riyafa Abdul Hameed
Riyafa Abdul Hameed

Reputation: 7983

WSO2 Script mediator: replace function with regex throws exception

I have the following replace expression in my inline WSO2 script mediator:

var cellId=mc.getProperty("RetreiveCellId");
cellId=cellId.replace(/\d+/, function(n){ return ++n });

where I am trying to increment the first number in the cellId. The cellId is of the form "R2C2" and once incremented it would be R3C2. This works fine in javascript tried with html. But throws the following exception in the inline script mediator:

[2015-11-25 21:20:20,216] ERROR - ScriptMediator The script engine returned an error executing the inlined js script function mediate
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EvaluatorException: Cannot convert /d+/ to java.lang.Character (<Unknown Source>#5) in <Unknown Source> at line number 5

What could be the possible reason for this? Is regex not supported in the script mediator? If so what alternative ways are there to achieve the same?

Upvotes: 2

Views: 1391

Answers (1)

Milad Kianmehr
Milad Kianmehr

Reputation: 347

Javascript is an engine for execute your java code. So you cannot use lots of Javascript functions. I solved same problem.

<script language="js">
var cellId=13333;
power=java.lang.Math.floor(java.lang.Math.log10(cellId)); 
addition=java.lang.Math.pow(10,power);  
cellId = java.lang.Integer(cellId+addition);            
mc.setProperty("test",cellId);
</script>

Excuse me about the algorithm if it's not good. I want to show you using java namespace.

Upvotes: 1

Related Questions