用正则表达式替换图像路径 [英] replacing image path with regular expression

查看:212
本文介绍了用正则表达式替换图像路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:



我有大量的html代码, img src =../ media / 2010/01 / something.jpg/>



img src =../ media / logo.png/>



我想用正则表达式做的是找到每个图像路径并替换为:



< img src =../ img / FILENAME.EXTENSION/>



我知道这是正确的表达式,但它只是不是我的一杯茶,可以帮助我吗?



Cheers,Mart

解决方案

这可能不是最好的解决方案,但它可能会工作:

 (< img。*?src =)([^] *?(\ / ] * \。[^] +))

然后您使用捕获组1和3创建新字符串(取决于风味):

  $ 1 ../ img $ 3 

您可以在此处看到它: http://regexr.com?2v8ir


I have massive html code, with loooads of images, problem is, every single image has a different path, example:

<img src="../media/2010/01/something.jpg" />

<img src="../media/logo.png" />

What I wanted to do with regular expressions is, to find every image path and replace it with:

<img src="../img/FILENAME.EXTENSION" />

I know that it's definately possible with regular expressions ... but it's just not my cup of tea, could any1 help me please?

Cheers, Mart

解决方案

This might not be the best solution but it might work:

(<img.*?src=")([^"]*?(\/[^/]*\.[^"]+))

and then you use capture group 1 and 3 to create the new string (depending on flavor):

$1../img$3

You can see it in action here: http://regexr.com?2v8ir

这篇关于用正则表达式替换图像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆