Search code examples
freemarker

Remove special characters in freemarker


I have a problem with Freemarker. I want to remove all the special characters from text string in freemarker: "!"#$%&'()*+,-./:;<=>?@[]^_`{|}~". I tried writing regular expression for this but it is not working and facing some errors:

<#assign s = 'Foo bAr$%,*^%@()":& baar'>

${s?replace('["!"#$%&'()*+,-./\:;<=>?@[]^_`{|}~"]', '', 'r')}

Please help


Solution

  • You could simplify this solution like this unless there are other special characters you explicitely want to keep:

    ${s?replace("[^\\w]|_", "", "r")}