RegEx replacing IMG src attributes

久未见 提交于 2019-12-11 02:07:05

问题


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

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

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

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

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

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

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


回答1:


You can do

Regex: (<IMG[^>]*)src="[^"]*c:.whatever.mybasedir.

Replace with:$1src="




回答2:


Replace (<IMG.*src=")(.*[/\\])(root[/\\].*?".*>)

with $1$3

EDIT

Hope this will work

Replace (<IMG.*src=")(.*[/\\]mybasedir[/\\])(root)(([/\\][^/\\]+)*)(".*>)

with $1$3$4$6



来源:https://stackoverflow.com/questions/14317027/regex-replacing-img-src-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!