`
maria3905
  • 浏览: 21636 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

freemarker的html转义

    博客分类:
  • web
 
阅读更多

 

 1.freemarker标签使用过程中,有一种情况,若是一个值中包含有html样式,在页面显示的时候直接${datail}显示的是错误的,



 

这样肯定是不正确的,有的人说加上?html,这种写法获得的结果是下图,直接把样式都显示出来了:



 

后来想了一种方法,采用正则表达式替换的方式,吧html标签都取消了,主要是采用freemarker的replace

函数,使用正则表达式,去除html标签,${datail?replace("<[^>]*>","","ri")},他的意思参考:

这样就能获取正确格式:若是遇到多层的函数,则优先转化,eg${(datail?replace("<[^>]*>","","ri"))[0..100]}

先转化再截取字符串



 

  1. 替换字符串 replace  
  2. ${s?replace(‘ba’, ‘XY’ )}  
  3. ${s?replace(‘ba’, ‘XY’ , ‘规则参数’)}将s里的所有的ba替换成xy 规则参数包含: i r m s c f 具体含义如下:  
  4. · i: 大小写不区分.  
  5. · f: 只替换第一个出现被替换字符串的字符串  
  6. · r:  XY是正则表达式  
  7. · m: Multi-line mode for regular expressions. In multi-line mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the string. By default these expressions only match at the beginning and the end of the entire string.  
  8. · s: Enables dotall mode for regular expressions (same as Perl singe-line mode). In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.  
  9. · c: Permits whitespace and comments in regular expressions. 
    另外补充一点:
    ([^>]*)([^<]*)这个通配符可以找到<和<符号间的字符串。
    <([^>]*)([^<]*)>这个通配符可以找到<和>符号间的字符串,作用可能是过滤网页上的html代码,包括图片和链接,仅留下纯文本的内容。

 

  • 大小: 27.2 KB
  • 大小: 15.9 KB
  • 大小: 6.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics