简体   繁体   中英

freemarker replace & and &

I have a problem with Freemarker. I want to remove all the special characters from this sentence, and also some similar sentences in the future:

BLA BLA RANDOM &, RANDOM BLA

In particular, the &, but the platform also uses HTML, so with the below code:

> <#assign text1 = name?replace("[^a-zA-Z0-9. ]", "",'r')>
>${text1}

I get:

BLA BLA RANDOM amp RANDOM BLA

i'm trying also something like this that it remove the &amp but how do i add also some special like !@#$%^90 to be removed in case will be in there?

> <#assign text1 = name?replace('&amp;,', '')>

I'm not sure what characters or character sequences you need to filter out, but based on what you already have, maybe this:

${s?replace('&[a-zA-Z]+;', '', 'r')?replace('[^a-zA-Z0-9. ]', '', 'r')}

So this first removes things like &amp; , then only keeps the "safe" characters.

But, at least in principle this is incomplete, because HTML can contain things like &#65; , which just means A , and so it shouldn't be removed, but replaced with A ... Or, it may contains tags like <b>foo</b> , which then should be converted to foo , not bfoob . Not sure if you need to handle such things, but if so, the string needs to be converted to plain text, which is not a trivial thing and FreeMarker has no built-in for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM