FreeMarker ?replace multiple values

Ali Torbati picture Ali Torbati · Jul 9, 2013 · Viewed 20k times · Source

Is it possible to replace multiple values in a FreeMarker template? For example, if I want to replace "a" and "b" with "c", I have to do this:

${event.EventTitle?replace('a','c')?replace('b','c')}

but I'd rather do something like this:

${event.EventTitle?replace("'a','b'",'c')}

Any chance FreeMarker has this capability?

I'm ultimately trying to replace all special characters ($,.,@,&,etc) with dashes, so feel free to suggest an easier way to do that.

Answer

ddekany picture ddekany · Jul 9, 2013

You could use a regular expression for this:

${event.EnventTitle?replace('a|b', 'c', 'r')}

Note the 'r' at the end.