2016-12-23 2 views
0

Я хочу, чтобы удалить начальные или конечные пробелы из строки, за исключением тега HTMLУдалить начальные или конечные пробелы из строки, за исключением тега 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}</, "<") 

Как удалить пробелы зависит от шаблона.

Есть ли лучший способ написать его?

ответ

0

Использование positive lookarounds:

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

Вышеуказанные средства «удалить все пробелы после '>' или перед '<'»

+0

Впервые я знал позитивные образы. – mikami

+0

спасибо, что сообщите мне. – mikami

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

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

О, метод, который я впервые узнал. Существуют также методы, подобные этому. Спасибо! – mikami

+0

Ни 'squish', ни' squeeze' не реализуют запрошенные функции. Этот ответ просто неверен. – mudasobwa

Смежные вопросы