UbbCode用正则替换指定格式的输入实现想要的输出

前端之家收集整理的这篇文章主要介绍了UbbCode用正则替换指定格式的输入实现想要的输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码如下

  1. public static String replaceLink(String strContent){
  2. Pattern pattern=null;
  3. Matcher matcher= null;
  4. if(strContent.indexOf("url")!=-1){
  5. String reGex="\\[url\\](http|https)(\\:\\/{2}\\w+\\.tt.cn.*?)\\[\\/url\\]";
  6. strContent = ubbReplace(strContent,reGex,"<a style=\"color:#5193C7;\" href=\"$1$2\" target=\"_blank\" >$1$2 </a>",pattern,matcher,true);
  7. reGex="\\[url\\](http|https):(\\/{2}\\w+\\.\\w+\\.\\w+\\.\\w+.*?)\\[\\/url\\]";
  8. strContent = ubbItem(strContent,"xxxxxxxxxxx",true);
  9. }
  10. return strContent;
  11. }
  12.  
  13. public static String ubbReplace(String strContent,String re,String replayStr,Pattern pattern,Matcher matcher,boolean IgnoreCase) {
  14. if (IgnoreCase) {
  15. pattern = Pattern.compile(re,Pattern.CASE_INSENSITIVE);
  16. }else{
  17. pattern = Pattern.compile(re);
  18. }
  19. matcher = pattern.matcher(strContent);
  20. strContent = matcher.replaceAll(replayStr);
  21. return strContent;
  22. }

猜你在找的正则表达式相关文章