如何使用JS 与正则表达式提取一段字符串中的 URL地址

jdayan 2009-08-31 03:34:34
使用一个网页编辑小工具FCKeditor,需要将插入文中的图片及flash地址提取单独保存,如果在插入的时候保存则不能监听到后面的删除,所以只能在用户保存的时候处理 整段的HTML代码,提取其中的图片与flash的URL地址保存。。
如:<p><embed src="/WebSite2/Upload/flash/890.swf" type="application/x-shockwave-flash" play="true" loop="true" menu="true"></embed><img height="391" width="480" alt="" src="/WebSite2/Upload/image/hello.jpg" /><img alt="" src="http://localhost:3375/WebSite2/fckeditor/editor/images/smiley/20090515/115.gif" /></p>
截取其中的 图片和flash的 URL地址
...全文
711 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hookee 2009-08-31
  • 打赏
  • 举报
回复

s = "<p> <embed src=\"/WebSite2/Upload/flash/890.swf\" type=\"application/x-shockwave-flash\" play=\"true\" loop=\"true\" menu=\"true\"> </embed> <img height=\"391\" width=\"480\" alt=\"\" src=\"/WebSite2/Upload/image/hello.jpg\" /> <img alt=\"\" src=\"http://localhost:3375/WebSite2/fckeditor/editor/images/smiley/20090515/115.gif\" /> </p>"
re = /src="([^"]+.swf)"/ig;
while((arr=re.exec(s))!=null){
alert(arr[1])
}
re = /src="([^"]+(.jpg|.gif|.jpeg|.png))"/ig;
while((arr=re.exec(s))!=null){
alert(arr[1])
}
jdayan 2009-08-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jdayan 的回复:]
非常感谢上面二位哈~~
还能不能麻烦二位一下呢
我这想将图片与flash 分开匹配查找
这样的正则表达式该怎么写呢?
[/Quote]
就是 这样分开的两个正则表达式该怎么写呢?
jdayan 2009-08-31
  • 打赏
  • 举报
回复
非常感谢上面二位哈~~
还能不能麻烦二位一下呢
我这想将图片与flash 分开匹配查找
这样的正则表达式该怎么写呢?
hookee 2009-08-31
  • 打赏
  • 举报
回复

s = "<p> <embed src=\"/WebSite2/Upload/flash/890.swf\" type=\"application/x-shockwave-flash\" play=\"true\" loop=\"true\" menu=\"true\"> </embed> <img height=\"391\" width=\"480\" alt=\"\" src=\"/WebSite2/Upload/image/hello.jpg\" /> <img alt=\"\" src=\"http://localhost:3375/WebSite2/fckeditor/editor/images/smiley/20090515/115.gif\" /> </p>"
re = /src="([^"]+)"/ig;
while((arr=re.exec(s))!=null){
alert(arr[1])
}
s_liangchao1s 2009-08-31
  • 打赏
  • 举报
回复

<script type="text/javascript">
<!--
var str = 's<p> <embed src="/WebSite2/Upload/flash/890.swf" type="application/x-shockwave-flash" play="true" loop="true" menu="true"> </embed> <img height="391" width="480" alt="" src="/WebSite2/Upload/image/hello.jpg" /> <img alt="" src="http://localhost:3375/WebSite2/fckeditor/editor/images/smiley/20090515/115.gif" /></p>';

var reg = /src\s*=\s*("|')([^"]+?)\1/g;

while((result = reg.exec(str)) != null){
alert(result[2])
}

//-->
</script>

87,904

社区成员

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

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