先把变量str做toLowerCase,在替换就可以了

解决方案 »

  1.   

    试试Pattern类的compile方法compile
    public static Pattern compile(String regex,
                                  int flags)Compiles the given regular expression into a pattern with the given flags. 
    Parameters:
    regex - The expression to be compiled
    flags - Match flags, a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, and CANON_EQ 
    Throws: 
    IllegalArgumentException - If bit values other than those corresponding to the defined match flags are set in flags 
    PatternSyntaxException - If the expression's syntax is invalid
      

  2.   

    import java.util.regex.*;String str    = "J2EE学习笔记J2ee and j2ee";
    String search = "j2ee";
    String repl   = "<font color='red'>" + search + "</font>";Pattern p = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(str);String str = m.replaceAll(repl);
      

  3.   

    没有现成的方法,只好用正则表达式喽。
    str = str.replaceAll("(J|j)2(E|e)(E|e)",repl);
      

  4.   

    String search = "j2ee";
    String repl   = "<font color='red'>" + search + "</font>";因为你使用了这两个式子,这就决定了,你替换后,所有的j2ee都变成小写的了,
    这和一楼说的有什么区别。
      

  5.   

    可能是我表达的有问题,写的也有问题吧,我的愿意就是把原字符加亮,忽略大小写。
    我刚才GOOGLE一下,java查出来有Java,也有java。
    我要的就是这个效果,多谢各位帮忙。
      

  6.   

    import java.util.regex.*;String str="J2EE学习笔记J2ee and j2ee";
    String search="j2ee";Pattern p = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(str);StringBuffer sb = new StringBuffer();
    while (m.find())
    {
        m.appendReplacement(sb, "<font color=\"red\">"+str.substring(m.start(), m.end())+"</font>");
    }m.appendTail(sb);
    System.out.println(sb.toString());
      

  7.   

    太感谢cm4ever(小P) 了。结帖。