求一个正则表达式,谢谢 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>"); } $(document).ready(function()在ie里不执行,ff和chrome可以执行, jquery 求助 下面这段代码是鼠标经过即切换选项卡,如何更改为点击才切换选项卡? http://tiaotiaotu.com/index.asp 上面导航栏颜色变化怎么做的 当点击一个按钮的是怎么把另一个消失掉 如何判断复选框被选中 constructor的困惑 如何获取<a href="">和</a>之间的内容? 缺少对象,为什么? 模式窗口中怎么关闭父窗口? 如何操作xmldom? 怎么把保存在数据库中的格式也显示出来? 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>");
}