您的位置:首页 > 其它

获取字符串中链接地址并替换为想要的内容

2015-01-26 10:51 274 查看
原帖地址: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中的前缀,若不需要去除即可
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: