现想把一个字符串
String s1 = " <span><imgwidth=39 height=17 </imgwidth=32></span>";
或者类似:
String s2 = " <span><imgwidth=39 height=17 </imgwidth=35></span>";
替换成正常的标签格式,如下:
<span><img width=39 height=17 </img></span>
第一个位置好替换,直接s1.replace("<imgwidth=", "<img width=")就能得到
<img width=39,
那么第二个应该要使用正则表达式替换吧,
意思就是把</imgwidth=35>替换成正常</img>
求助大神指点啊。。。。
第二处
"<//imgwidth=//d//d>替换为"<//img>"
public static void main(String[] args) {
String s1 = " <span><imgwidth=39 height=17 </imgwidth=32></span>";
String s2 = s1.replaceAll("</imgwidth=(\\d{2})>", "</img>");
System.out.println("原字符串:" + s1);
System.out.println("替换后的字符串:" + s2);
}
你前面的字符串已经替换了,我这就只写了替换后面的,,你可以把代码拷贝过去运行看看