求一正则表达式的java程序方法.
public void apendCount(String url, String appendStr);
只要html内的a标签的href的值匹配url,则在标签里追加appendStr.如:
<a href="http://www.baidu.com" target="_blank">百度</a>
当调用方法appendCount("http://www.baidu.com","X")时,
html内的<a href="http://www.baidu.com" target="_blank">百度</a>
被更改为<a href="http://www.baidu.com" target="_blank">X百度</a>
感激不尽~~~
public void apendCount(String url, String appendStr);
只要html内的a标签的href的值匹配url,则在标签里追加appendStr.如:
<a href="http://www.baidu.com" target="_blank">百度</a>
当调用方法appendCount("http://www.baidu.com","X")时,
html内的<a href="http://www.baidu.com" target="_blank">百度</a>
被更改为<a href="http://www.baidu.com" target="_blank">X百度</a>
感激不尽~~~
解决方案 »
- 使用struts-Jquery插件,怎么在Action中设select的selectedindex?
- poi 导出excel 类型转换问题
- 关于旅行网的运营模式讨论
- wenlogic问题
- ireport 报表问题,求大虾忙帮,谢谢!!!!
- myeclipse下birt开发,懂得来给点建议
- FCKEDITOR的安装问题,高手进来
- 100分求eclipse开发的JSP为什么部署到tomcat下不能运行。。但是部署到weblogic下是正常的/
- mysql5.1的默认连接用户名和密码是多少?
- 30分!求BreakIterator的用法..
- struts 2
- 页面JS与table互动的问题,在线等,各位大侠好心帮帮忙
String s = "<a href=\"http://www.baidu.com\" target=\"_blank\">百度 </a>";
System.out.println(url.replaceAll("(<.*>)(.*)(</.*>)", "$1" + appendStr + "$2$3"));
System.out.println(url.replaceAll("(<.*>)(.*)(</.*>)", "$1" + appendStr + "$2$3"));
我开始是这样做的:
public class TestPage
{
public static String tContent(String content, String url, String appendStr)
{
String regex = "(?x)(?i)((<a\\s)([^<>]*)href\\s*=\\s*('|\")" + url + "('|\")([^<>]*)[>])";
return content.replaceAll(regex, "$1" + appendStr);
// Pattern pattern = Pattern.compile(regrex);
// Matcher matcher = pattern.matcher(content);
//
// String part = "";
// if (matcher.find())
// {
// part = matcher.group(0);
// return content.replaceAll(regrex, part + appendStr);
// }
// return content;
}
public static void main(String[] args)
{
String content = "<div><a href='http://www.baidu.com'>百度</a><div>";
String url = "http://www.baidu.com";
System.out.println(TestPage.tContent(content, url, "这里通向"));
}