Javasript command (replace) not working in OH

I’m starting to transfer my rules from node red to oh. I’m not very familiar with Javascript but managed to get things working in node red.
Following code gets a string value (e.g. “560 ppm”) and then I want to get rid of the characters, that’s what the second line is supposed to do. In OH it doesn’t work though. Could you tell me why, please?

z = itemRegistry.getItem("NetatmoEsszimmer_CO2Gehalt").getState();
x = z.replace(/[^0-9]/g,'');    //doesn't work

getState() is returning a state object. It may look like a string, and some parts of OH maybe able to treat it as if it were a string, but in rules you still need to cast it explicitly to a string using getState().toString() before you can use string methods on it.

Also, if the formatting of this value is consistent with the space between value and unit, you may find it easier to use split() instead of replace().

1 Like

Thank you, I wasn’t aware of that.
Even nicer than split is in this case:
x = parseFloat(itemRegistry.getItem(“NetatmoEsszimmer_CO2Gehalt”).getState()) ;
Surprised me that it’s working, but it extracts the number and simply ignores the text