特殊字符导致用正则表达式进行字符串替换失败,Java replaceAll()方法报错Illegal group reference

String str = "给商品||?>\n阳澄湖大闸蟹!@#$%^&*()_+-=?:",.]\|~.,/??\\|\br点赞"
1 String tradeDescription = "||?>\n阳澄湖大闸蟹!@#$%^";
2 String description = "给商品 #goodsName# 点赞";
3 String result = description.replaceAll("#goodsName#", Matcher.quoteReplacement(CommonUtil.toString(tradeDescription)));

参考:

http://www.colabug.com/thread-1131948-1-1.html

原文地址:https://www.cnblogs.com/xunux/p/4855860.html