user22734805
user22734805

Reputation:

Jexl toLowerCase() and replace()

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

Upvotes: 1

Views: 306

Answers (1)

mandy8055
mandy8055

Reputation: 6735

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

Upvotes: 0

Related Questions