简体   繁体   English

使用正则表达式删除相对路径斜杠

[英]Using regular expressions to remove relative path slashes

I am trying to remove all the relative image path slashes from a chunk of HTML that contains several other elements. 我正在尝试从包含其他几个元素的HTML块中删除所有相对图像路径斜杠。

For example 例如

<img src="../../../../images/upload/1/test.jpg />

would need to become 将需要成为

<img src="http://s3.amazonaws.com/website/images/upload/1/test.jpg" />

I was thinking of writing this as a rails helper, and just passing the entire block into the method, and make using Nokogiri or Hpricot to parse the HTML instead, but I don't really know. 我当时想将其编写为Rails的帮助程序,只是将整个代码块传递给方法,然后使用Nokogiri或Hpricot来解析HTML,但是我并不知道。

Any help would be great 任何帮助都会很棒

Cheers Adam 干杯亚当

One way to construct an absolute path given the absolute URL of the page and a relative path found on that page: 给定页面的绝对URL和在该页面上找到的相对路径的一种构造绝对路径的方法:

pageurl = 'http://s3.amazonaws.com/website/foo/bar/baz/quux/index.html'
relative = '../../../../images/upload/1/test.jpg'
absolute = pageurl.sub(/\/[^\/]*$/, '')
relative.split('/').each do |d|
  if d == '..'
    absolute.sub!(/\/[^\/]*$/, '')
  else
    absolute << "/#{d}"
  end
end
p absolute

Alternatively, you could cheat a bit: 或者,您可以作弊:

'http:/'+File.expand_path(File.dirname(pageurl.sub(/^http:/, ''))+'/'+relative)

No need to reinvent the wheel, when the builtin 'uri' lib can do that for you: 当内置的“ uri”库可以为您做到这一点时,无需重新发明轮子:

require 'uri'
main_path = "http://s3.amazonaws.com/website/a/b/c"
relative_path = "../../../../images/upload/1/test.jpg"

URI.join(main_path, relative_path).to_s
  # ==> "http://s3.amazonaws.com/images/upload/1/test.jpg"

This chunk might help: 此块可能会帮助:

html = '<img src="../../../../images/upload/1/test.jpg />'
absolute_uri = "http://s3.amazonaws.com/website/images"
html.gsub(/(\.\.\/)+images/, absolute_uri)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM