匹配下面的模式:
[black]something[/black] [orange]something[/orange]
然後改為HTML代碼:<span style="color:black">something</span>
但是並沒有配對修改成 [black]hello[/orange]
怎麼使用正則表達式來識別?
public static String bbcode(String text) {
String html = text;
Map<String,String> bbMap = new HashMap<String , String>();
bbMap.put("\\[black\\](.+?)\\[/black\\]", "<span style='color: black;'>$1</span>");
bbMap.put("\\[orange\\](.+?)\\[/orange\\]", "<span style='color: orange;'>$1</span>");
// add other colors
for (Map.Entry entry: bbMap.entrySet()) {
html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
}
return html;
}