原帖地址:http://bbs.csdn.net/topics/370029975#quote
要替换的字符串:
http://www.xxx.com/1噶为欧冠奇偶位i二姐http://www.xxx.com/1/2乌黑欧冠我几乎http://www.xxx.com/1/2/3的稿费欧文和岗位i偶尔给http://www.xxx.com/1.jpg
java代码:
public static void main(String[] args) {
String str = "http://www.xxx.com/1噶为欧冠奇偶位i二姐http://www.xxx.com/1/2乌黑欧冠我几乎http://www.xxx.com/1/2/3的稿费欧文和岗位i偶尔给http://www.xxx.com/1.jpg";
str = str.replaceAll("(?is)(http://[/\\.\\w]+\\.jpg)","<img src='$1'/>");
str = str.replaceAll("(?is)(?<!')(http://[/\\.\\w]+)","<a href='$1'>$1</a>");
System.out.println(str);
}
另附jS代码:
function filterShortUrl(html){
return html.replace(/http:\/\/t.cn\/\w+/g,"<a href='$&' target='_blank'>$&</a>"); // 其中t.cn为url中的前缀,若不需要去除即可
}