[id121212|aaaaaa]zzzzz [id12123|werweref]zzzzzzzzz模式匹配
JAVA 处理方式:
public static String formatComments(String str){
if(str != null && !str.equals("")){
String format="\\[id\\d+\\|[^\\]]+\\]";
Pattern pattern = Pattern.compile(format,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
String result="";
result = result.copyValueOf(str.toCharArray());
while(matcher.find()){
String id_and_name=str.substring(matcher.start(), matcher.end());
String name = id_and_name.substring(id_and_name.indexOf("|")+1, id_and_name.length()-1);
result = result.replace(id_and_name, name);
}
return result;
}
return str;
}
JS处理方式:
方法一:
function formatStr(str){
var pattern = /\[id\d+\|[^\]]+\]/g;
var result;
while((result = pattern.exec(str)) != null)
{
var str1 = result[0];
var str2 = str1.substring(str1.indexOf("|")+1, str1.length-1);
str = str.replace(str1,str2);
}
return str;
}
方法二:
var replaceTrim = function(str){
if( str.indexOf('[id')!=-1 ){
var other_str = str.replace(/\[id\d+\|[^\]]+\]/g,'^ca^');
other_str = other_str.split('^ca^');
var array = str.match(/\[id\d+\|[^\]]+\]/g);
for ( var i=0;i<array.length;i++ ){
var s = array[i]+"";
array[i] = s.substring(s.indexOf('|')+1,s.length-1);
}
var c = "";
for( var i=0;i<other_str.length;i++ ){
other_str[i] = other_str[i] || "";
array[i] = array[i] || "";
c += other_str[i]+array[i];
}
return c;
}else{
return str;
}
}