除了ruby中的html标记之外,删除字符串的前导或尾随空格

时间:2016-12-23 12:46:49

标签: ruby regex

我想删除除html标记

之外的字符串的前导或尾随空格

例如

html = <a class=\"c-grid__quotation--link\" target=\"_blank\" href=\"https://www.yahoo.com/\"><div class=\"c-grid__quotation text--s-md p-topic__quotation__border c-border-r-5\">\n  <div class=\"c-flex\">\n    <div class=\"c-grid__quotation--main\">\n      <img src=\"https://s.yimg.com/dh/ap/default/130909/y_200_a.png\" alt=\"Y 200 a\" />\n    </div>\n    <div class=\"c-grid__quotation--side\">\n      <div class=\"c-grid__quotation--side-title text--b\">\n        Yahoo\n      </div>\n      <div class=\"c-grid__quotation--side-description\">\n        News, email and search are just the beginning. Discover more every day. Find your yodel.\n      </div>\n      <div class=\"c-grid__quotation--side-url\">\n        www.yahoo.com\n      </div>\n    </div>\n  </div>\n</div></a>

我这样做的方式

html.gsub(/>\s{1,8}</, "><").gsub(/>\s{1,8}/, ">").gsub(/\s{1,8}</, "<")

如何删除空白取决于模式。

有没有更好的方法来写它?

2 个答案:

答案 0 :(得分:1)

使用positive lookarounds

html = %| <a class=\"c-.......| # your line goes here
html.gsub(/(?<=>)\s+|\s+(?=<)/, '')

以上表示“在'>'之后或'<'之前删除所有空格。”

答案 1 :(得分:-1)

尝试以下:

html = "<a class=\"c-grid__quotation--link\" target=\"_blank\" href=\"https://www.yahoo.com/\"><div class=\"c-grid__quotation text--s-md p-topic__quotation__border c-border-r-5\">\n  <div class=\"c-flex\">\n    <div class=\"c-grid__quotation--main\">\n      <img src=\"https://s.yimg.com/dh/ap/default/130909/y_200_a.png\" alt=\"Y 200 a\" />\n    </div>\n    <div class=\"c-grid__quotation--side\">\n      <div class=\"c-grid__quotation--side-title text--b\">\n        Yahoo\n      </div>\n      <div class=\"c-grid__quotation--side-description\">\n        News, email and search are just the beginning. Discover more every day. Find your yodel.\n      </div>\n      <div class=\"c-grid__quotation--side-url\">\n        www.yahoo.com\n      </div>\n    </div>\n  </div>\n</div></a>"

- &GT; html.squeeze(' ').strip

<强>输出:

"<a class=\"c-grid__quotation--link\" target=\"_blank\" href=\"https://www.yahoo.com/\"><div class=\"c-grid__quotation text--s-md p-topic__quotation__border c-border-r-5\"> <div class=\"c-flex\"> <div class=\"c-grid__quotation--main\"> <img src=\"https://s.yimg.com/dh/ap/default/130909/y_200_a.png\" alt=\"Y 200 a\" /> </div> <div class=\"c-grid__quotation--side\"> <div class=\"c-grid__quotation--side-title text--b\"> Yahoo </div> <div class=\"c-grid__quotation--side-description\"> News, email and search are just the beginning. Discover more every day. Find your yodel. </div> <div class=\"c-grid__quotation--side-url\"> www.yahoo.com </div> </div> </div> </div></a>"