求一个正则表达式,谢谢 http://www.web521.com/web/577668/T660534.shtml 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <script language=javascript> var str="[SIZE=5]字号的大小[/SIZE]" var str2="字体颜色" re=/\[(.+?)=(.+?)\](.*?)\[\/\1\]/g str=str.replace(re,"<font style=\"$1:$2\">$3<\/font>") alert(str) str2=str2.replace(re,"<font style=\"$1:$2\">$3<\/font>") alert(str2)</script> 再加50分,上面这位大虾,能不能给一个java的写法,$1:$2这样,在java里好像不适用啊 引的包都是正则的包Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]"); Matcher m = p.matcher("[SIZE=5]字号的大小[/SIZE]"); if(m.matches()){ System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>")); } m = p.matcher("字体颜色"); if(m.matches()){ System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>")); } 这个SIZE和COLOR不是固定值啊,这样写不行吧,也有可能是size=7,8,9等等,我都写进去也不行啊? 再加50,呵呵,我着急; Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]"); Matcher m = p.matcher("[SIZE=7]字体颜色[/SIZE]"); if(m.matches()){ System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>")); }这句运行不正确啊 可以啊,不过这个要具体情况具体加的,举个反例,再向上加条件Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]"); Matcher m = p.matcher("[SIZE=7]字号的大小[/SIZE]"); if(m.matches()){ System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>")); } } 大体上这样吧,具体分支你在改改String str="[SIZE=7][SIZE=5]字体颜色[/SIZE][/SIZE]"; Pattern p = Pattern.compile("\\[(.+?)=([^\\]]+?)\\]([^\\[]+)(\\[(.+?)\\])+"); Matcher m = p.matcher(str); String innerHTML=null; String innerText=null; if(m.matches()){ innerHTML=m.replaceAll("\"$1:$2;\""); innerHTML=innerHTML.replaceAll("\\]\\[",";"); p=Pattern.compile("(\\[[^\\]]+\\])+([^\\[]+)(\\[([^\\]]+?)\\])+"); m=p.matcher(str); if(m.matches()){ innerText=m.replaceAll("$2"); } System.out.println("<font style="+innerHTML+">"+innerText+"</font>"); } 请教为什么这段你代码中的else if 为什么没有执行呢? Juery在IE6下类选择器和Id选择器的性能 创建一个按钮 触发事件..... 一个基础问题 关于上传下载的问题 脚本双引号转义? 急用,不用iframe如何改变层中的内容? 关于radiobutton获得焦点的一个问题 请问如何在服务器端的Jscript 中 判断一个session 变量存不存在? zhu_liping(zz)来拿分 怎么把保存在数据库中的格式也显示出来? JavaScript问题,求助
var str="[SIZE=5]字号的大小[/SIZE]"
var str2="字体颜色"
re=/\[(.+?)=(.+?)\](.*?)\[\/\1\]/g
str=str.replace(re,"<font style=\"$1:$2\">$3<\/font>")
alert(str)
str2=str2.replace(re,"<font style=\"$1:$2\">$3<\/font>")
alert(str2)
</script>
Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]");
Matcher m = p.matcher("[SIZE=5]字号的大小[/SIZE]");
if(m.matches()){
System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>"));
}
m = p.matcher("字体颜色");
if(m.matches()){
System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>"));
}
Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]");
Matcher m = p.matcher("[SIZE=7]字体颜色[/SIZE]");
if(m.matches()){
System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>"));
}这句运行不正确啊
Pattern p = Pattern.compile("\\[(.+?)=(.+?)\\](.*?)\\[\\/(.+?)\\]");
Matcher m = p.matcher("[SIZE=7]字号的大小[/SIZE]");
if(m.matches()){
System.out.println(m.replaceAll("<font style=\"$1:$2\">$3</font>"));
}
}
String str="[SIZE=7][SIZE=5]字体颜色[/SIZE][/SIZE]";
Pattern p = Pattern.compile("\\[(.+?)=([^\\]]+?)\\]([^\\[]+)(\\[(.+?)\\])+");
Matcher m = p.matcher(str);
String innerHTML=null;
String innerText=null;
if(m.matches()){
innerHTML=m.replaceAll("\"$1:$2;\"");
innerHTML=innerHTML.replaceAll("\\]\\[",";");
p=Pattern.compile("(\\[[^\\]]+\\])+([^\\[]+)(\\[([^\\]]+?)\\])+");
m=p.matcher(str);
if(m.matches()){
innerText=m.replaceAll("$2");
}
System.out.println("<font style="+innerHTML+">"+innerText+"</font>");
}