Search code examples
replacefreemarker

FreeMarker ?replace multiple values


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.


Solution

  • You could use a regular expression for this:

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

    Note the 'r' at the end.