Forum Discussion

kavithams's avatar
7 years ago

Freemarker replace string operation does not work inside a function

We need to replace certain characters in a string in freemarker. Generally we do this. <#assign message = message?replace("old","new") />   But this replace operation does not work when used with ...
  • TariqHussain's avatar
    TariqHussain
    7 years ago

    kavithams - Small edit in your code and it worked fine. Strange but you have to use a different variable inside a function.

     

    Before

     

    <#function replaceVal msg>
          <#assign msg = msg?replace('following ','FOLLOWING') />
          <#return msg />
    </#function>

     

     

    After

     

    <#function replaceVal msg>
          <#assign repleacedText = msg?replace('following ','FOLLOWING') />
          <#return repleacedText />
    </#function>

     

     

    Also, you if you want to ignore case-sensitive, you can pass another parameter  'i" as mentioned by PerBonomi

    E.g

     

    <#assign repleacedText = msg?replace('following ','FOLLOWING', 'i') />