[id121212|aaaaaa]zzzzz模式匹配

[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;
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值