后台获取得字符串:
<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>要转换成<p><img style="float:none;margin:0px;" alt="" src="htt://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="htt://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>要找出原字符串中的所有img标签的src属性例如:feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png,拼接成http://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png替换原来的src属性!难!!!
<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>要转换成<p><img style="float:none;margin:0px;" alt="" src="htt://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="htt://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>要找出原字符串中的所有img标签的src属性例如:feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png,拼接成http://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png替换原来的src属性!难!!!
解决方案 »
- 探讨网页编程中最难的一门语言。。。。。。
- JS实选定当前行checkbox
- 请教一个效果的实现 在线等
- select中,关于if(event.keyCode==13)event.keyCode=9的问题,帮助看看......
- 一个JSP接收参数 传参数问题
- 有关判断问题紧急求助,在线等。
- 遇到一个奇怪问题,关于HighCharts的全局变量的。
- 表格数据编辑
- ÂÒÊÀ¶ùÅ®(下载中...) 谁能告诉我它的中文是什么呀??????????????
- javascript 的疑问
- A页面有个链接 到 B 页面 B页面处理完了 刷新A 页面 A页面和B页面是平级的
- 各位我遇到了问题?我弄了个JS动态添加行,在IE正常,在火狐里有问题,不能添加行?求大家帮我看一下?
var s='<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>';
alert(s.replace(/<img (.*?)src="(.+?)"(.*?)>/,'<img $1src="http://$2"$3>'));
</script>
你写的那段js不能运行啊
var s='';
s= s.replace(/src="([^"]+)"/gi, 'src="http://$1"');
<head>
<script>
function rp(){
var h = document.getElementById("test").innerHTML;
alert(h.replace(/<img(.*?)src="[^http:\/\/](.+?)"(.*?)>/g,'<img$1src="http://$2"$3>'));}
</script></head>
<body>
<div id="test">
<p><img style="float:none;margin:0px;" alt="" src="http://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>
</div>
<input type="button" value="替换" onclick="rp()"/>
</body>
</html>
<html>
<head>
<title>无标题页</title>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var s = '<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>';
$("#btn").click(function() {
var patt = new RegExp(/<img[\s|\S|\s]*?src=[\s|\"|\']*([^>\"\']*)[\s|\S]*?>/ig);
while ((result = patt.exec(s)) != null) {
s = s.replace(result[1], "http://"+result[1]);
}
alert(s);
});
});
</script>
</head>
<body>
<input type="button" id="btn" onclick="test()" value="测试" />
</body>
</html>
刚弄错了 这个是对的 你可以测试下 直接复制 运行<html>
<head>
<script>
function gg(){
var s="<img src='feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png' />";
s= s.replace(/src=(')/gi, 'src=$1http://');
alert(s);
}
</script>
</head>
<body>
<a href="#" onclick="gg();">替换</a>
</body>
</html>
<head>
<script>
function gg(){
var s="<img src='feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png' />";
s= s.replace(/src=(')/g, 'src=$1http://');
alert(s);
}
</script>
</head>
<body>
<a href="#" onclick="gg();">替换</a>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>无标题页</title>
<script type="text/javascript">
function test() {
var s = '<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>';
var patt = new RegExp(/<img[\s|\S|\s]*?src=[\s|\"|\']*([^>\"\']*)[\s|\S]*?>/ig);
while ((result = patt.exec(s)) != null) {
s = s.replace(result[1], "http://" + result[1]);
}
alert(s);
}
</script>
</head>
<body>
<input type="text" id="txtVal" /> <input type="button" id="btn" onclick="test()" value="测试" />
</body>
</html>
var s='<p><img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /> 1111 <img style="float:none;margin:0px;" alt="" src="feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png" /></p>';
alert(s.replace(/<img (.*?)src="(.+?)"(.*?)>/ig,'<img $1src="http://$2"$3>'));
</script> 之前随手写的,没考虑大小写和多匹配g (全文查找出现的所有 pattern)
i (忽略大小写)
m (多行查找)