Jexl toLowerCase() and replace()

81 Views Asked by At

I am using jexl.eval function to evaluate a value as follows-

jexl.eval("dataset[0].data['Entity Name'].toLowerCase().replace(/ /g, '_')",data);

dataset[0].data['Entity Name'] is always a string and is never null, there seems to be some error in jexl.eval, It returns empty string always

I wanted to confirm if toLowerCase() and replace() are supported or not? whether there is some issue in my syntax?

jexl playground for next query

1

There are 1 best solutions below

3
mandy8055 On BEST ANSWER

toLowerCase() and replace are supported methods for string manipulation but you're using the wrong syntax which may be one of the reasons for unwanted output.

In JEXL, you can use the | (pipe) operator to chain transformations on a value. So, to properly use them, you need to define thee transform functions(for toLowerCase and replace) before using them in your JEXL expression. Something like:

jexl.eval("dataset[0].data['Entity Name'] | lower | replace(' ', '_')", data);

// Tranformations 
jexl.addTransform('lower', (val) => val.toLowerCase());
jexl.addTransform('replace', (val, search, replacement) => val.replace(new RegExp(search, 'g'), replacement));

JEXL playground