求一个复杂的正则表达式!

yangxufeng058 2011-12-20 10:15:18
后台获取得字符串:
<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属性!难!!!
...全文
107 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
周祥 2011-12-20
  • 打赏
  • 举报
回复
我那个没问题 src 如果用 单引号括起来 就用

s= s.replace(/src=(')/g, 'src=$1http://');

如果是双引号括起来 就用

s= s.replace(/src=(")/g, 'src=$1http://');
周祥 2011-12-20
  • 打赏
  • 举报
回复
一个简单的问题 非要复杂化 想表现什么???
周祥 2011-12-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wcfan 的回复:]
发错了 上面的代码 这个才OK的


HTML code


<!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/xhtm……
[/Quote]

你测试我的就知道了 你的搞的太复杂了
wcfan 2011-12-20
  • 打赏
  • 举报
回复
发错了 上面的代码 这个才OK的


<!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>

周祥 2011-12-20
  • 打赏
  • 举报
回复
再发一次

<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>
周祥 2011-12-20
  • 打赏
  • 举报
回复

刚弄错了 这个是对的 你可以测试下 直接复制 运行

<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>
wcfan 2011-12-20
  • 打赏
  • 举报
回复

<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>

p2227 2011-12-20
  • 打赏
  • 举报
回复
<html>
<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>
周祥 2011-12-20
  • 打赏
  • 举报
回复

var s='';
s= s.replace(/src="([^"]+)"/gi, 'src="http://$1"');
yangxufeng058 2011-12-20
  • 打赏
  • 举报
回复
这个url是不对的 只是举个例子的
你写的那段js不能运行啊
hch126163 2011-12-20
  • 打赏
  • 举报
回复
http://feedAttachment/attachment/19/1324343834390/feed_img_pview_500_400.png

这个url 也不对啊

<script>
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>
hch126163 2011-12-20
  • 打赏
  • 举报
回复
<script>
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 (多行查找)

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧