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 & but how do i add also some special like !@#$%^90 to be removed in case will be in there?
> <#assign text1 = name?replace('&,', '')>
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 &
, then only keeps the "safe" characters.
But, at least in principle this is incomplete, because HTML can contain things like A
, 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.