function f2c(s) { var test = /(\d+(\.\d*)?)℉/g; return(s.replace(test,function($0,$1,$2) { return((($1-32) * 5/9) + "℃"); } ) ); } alert(f2c("Water freezes at 32℉ and boils at 212℉."));如果在java中怎么写呢? java中的replace方法也很强大,只是不支持回调函数,所以就动手写了个简单的替换工具类:
/** * @author yeyong */public class StringUtils { // 就把它们放在StringUtils工具类中了 /** * 将String中的所有regex匹配的字串全部替换掉 * @param string 代替换的字符串 * @param regex 替换查找的正则表达式 * @param replacement 替换函数 * @return */ public static String replaceAll(String string, String regex, ReplaceCallBack replacement) { return replaceAll(string, Pattern.compile(regex), replacement); } /** * 将String中的所有pattern匹配的字串替换掉 * @param string 代替换的字符串 * @param pattern 替换查找的正则表达式对象 * @param replacement 替换函数 * @return */ public static String replaceAll(String string, Pattern pattern, ReplaceCallBack replacement) { if (string == null) { return null; } Matcher m = pattern.matcher(string); if (m.find()) { StringBuffer sb = new StringBuffer(); int index = 0; while (true) { m.appendReplacement(sb, replacement.replace(m.group(0), index++, m)); if (!m.find()) { break; } } m.appendTail(sb); return sb.toString(); } return string; } /** * 将String中的regex第一次匹配的字串替换掉 * @param string 代替换的字符串 * @param regex 替换查找的正则表达式 * @param replacement 替换函数 * @return */ public static String replaceFirst(String string, String regex, ReplaceCallBack replacement) { return replaceFirst(string, Pattern.compile(regex), replacement); } /** * 将String中的pattern第一次匹配的字串替换掉 * @param string 代替换的字符串 * @param pattern 替换查找的正则表达式对象 * @param replacement 替换函数 * @return */ public static String replaceFirst(String string, Pattern pattern, ReplaceCallBack replacement) { if (string == null) { return null; } Matcher m = pattern.matcher(string); StringBuffer sb = new StringBuffer(); if (m.find()) { m.appendReplacement(sb, replacement.replace(m.group(0), 0, m)); } m.appendTail(sb); return sb.toString(); }}
回调类及其一个抽象类:
/** * 字符串替换的回调接口 * * @author yeyong * */public interface ReplaceCallBack { /** * 将text转化为特定的字串返回 * @param text 指定的字符串 * @param index 替换的次序 * @param matcher Matcher对象 * @return */ public String replace(String text, int index, Matcher matcher);}
/** * 抽象的字符串替换接口 *主要是添加了$(group)方法来替代matcher.group(group) * @author yeyong * */public abstract class AbstractReplaceCallBack implements ReplaceCallBack { protected Matcher matcher; /* * (non-Javadoc) * @see utils.ReplaceCallBack#replace(java.lang.String, int, java.util.regex.Matcher) */ final public String replace(String text, int index, Matcher matcher) { this.matcher = matcher; try { return doReplace(text, index, matcher); } finally { this.matcher = null; } } /** * 将text转化为特定的字串返回 * @param text 指定的字符串 * @param index 替换的次序 * @param matcher Matcher对象 * @return */ public abstract String doReplace(String text, int index, Matcher matcher); /** * 获得matcher中的组数据 *
等同于matcher.group(group) *
该函数只能在{@link #doReplace(String, int, Matcher)} 中调用 * @param group * @return */ protected String $(int group) { String data = matcher.group(group); return data == null ? "" : data; }}
测试一下:
String string = "the quick-brown fox jumps over the lazy-dog."; String pattern = "\\b(\\w)(\\w*?)\\b"; // 将每个单词改为首字大写其他字母小写 System.out.println(StringUtils.replaceAll(string, pattern, new AbstractReplaceCallBack() { public String doReplace(String text, int index, Matcher matcher) { return $(1).toUpperCase() + $(2).toLowerCase(); } })); //输出:The Quick-Brown Fox Jumps Over The Lazy-Dog. // 将文本中类似aaa-bbb-ccc的替换为AaaBbbCcc string = "the quick-brown fox jumps over the lazy-dog. aaa-bbbb-cccc-ddd"; pattern = "\\b\\w+(?:-\\w+)+\\b"; System.out.println(StringUtils.replaceAll(string, pattern, new AbstractReplaceCallBack() { private ReplaceCallBack callBack = new AbstractReplaceCallBack() { public String doReplace(String text, int index, Matcher matcher) { return $(1).toUpperCase() + $(2).toLowerCase(); } }; public String doReplace(String text, int index, Matcher matcher) { return StringUtils.replaceAll(text, "(?:\\b|-)(\\w)(\\w*?)\\b", callBack); } })); // 输出: the QuickBrown fox jumps over the LazyDog. AaaBbbbCcccDdd //过滤安全字符... TODO 应提取为一个方法 final Mapmap = new HashMap () { private static final long serialVersionUID = 1L; { put("<", "<"); put(">", ">"); put("\"", """); put("'", "'"); } }; ReplaceCallBack callBack = new ReplaceCallBack() { public String replace(String text, int index, Matcher matcher) { return map.get(text); } }; string = "xxxxx 1<4 & 7>5"; System.out.println(StringUtils.replaceAll(string.replace("&", "&"), "[<>\"\']", callBack)); // 输出: <html><body>xxxxx 1<4 & 7>5</body></html>
实现上面js例子的替换:
// 实现上面js例子的替换 string = "Water freezes at 32℉ and boils at 212℉."; System.out.println(StringUtils.replaceAll(string, "(\\d+(\\.\\d*)?)℉", new AbstractReplaceCallBack() { public String doReplace(String text, int index, Matcher matcher) { return (((Double.parseDouble($(1)) - 32) * 5 / 9) + "℃"); } })); // 输出: Water freezes at 0.0℃ and boils at 100.0℃.