RegEx取代IMG src属性 [英] RegEx replacing IMG src attributes

查看:69
本文介绍了RegEx取代IMG src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 var baseURL 我知道是: baseURL = c:\无论\mybasedir\

我有一个HTML源代码,可能包含以下内容:

I have a var baseURL that I know is: baseURL = c:\whatever\mybasedir\
I have an HTML source code that may contain this:

<IMG alt="foo" src="file://c:\whatever\mybasedir\root\foo\bla.gif">
or/and:
<IMG alt="foo" src="file://c:/whatever/mybasedir/root/foo/bla.gif">
or/and:
<IMG src="c:\whatever\mybasedir\root\foo\bla.gif">
or/and: 
<IMG src="c:\whatever\mybasedir/root/foo/bla.gif">

我需要替换所有src标记,以便相对于 baseURL :

I need to replace all src tags so that result path is Unix style relative to baseURL:

<IMG src="root/foo/bla.gif">

或是否存在alt属性(或其他属性顺序可能有所不同):

or if there was an alt attribute (or other. order of attributes may vary):

<IMG alt="foo" src="root/foo/bla.gif">

我该如何匹配< IMG * src = * *> ;
有什么想法RegEx(或其他方法)可以在这里提供帮助吗?

How do I match <IMG * src="*" *>? Any ideas what RegEx (or other method) can help here?

(我无法使用DOM来完成此工作,因为IE8 / 9 DOM首先导致了这种情况-自动添加< base href> 到所有相关的src标记)

(I cannot use DOM to do this job, since the IE8/9 DOM is causing this situation in the first place - automatically adding <base href> to all relative src tags)

推荐答案

您可以

正则表达式:(< IMG [^>] *)src = [^] * c:.whatever.mybasedir。

替换为: $ 1src =

这篇关于RegEx取代IMG src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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