# Using gsub to remove embedded newlines in HTML file

**URL:** <https://www.ruby-forum.com/t/using-gsub-to-remove-embedded-newlines-in-html-file/101030>\
**Category:** Ruby\
**Created:** [August 3, 2006, 12:58am UTC](https://www.ruby-forum.com/t/using-gsub-to-remove-embedded-newlines-in-html-file/101030 "2006-08-03T00:58:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 3, 2006, 12:58am UTC](https://www.ruby-forum.com/t/using-gsub-to-remove-embedded-newlines-in-html-file/101030/1 "2006-08-03T00:58:49Z")

</div>

I have an HTML file that is in a string.

I want to use gsub! to recursively remove any embedded newlines and  
whitespace within two known delimeters.

Given a string that includes this kind of string:

~^LNK:[http://slashdot.org/login.pl?op=newuserform~](http://slashdot.org/login.pl?op=newuserform~)  
Create a new account  
^~

I want to replace the above with:

~^LNK:[http://slashdot.org/login.pl?op=newuserform~Create](http://slashdot.org/login.pl?op=newuserform~Create) a new account^~

(stripping out the newlines and whitespace)

Having trouble writing the regex for this.

I think I want something like:

/~^LNK:._?([\s\r\n])+._?^~/

that I could use in:

str.gsub!(/~^LNK:._?([\s\r\n])+._?^~/, ‘’)

to replace all of the whitespace, or potential newline characters with  
null strings.

But I don’t think this will work because I really need to loop _within_  
each substring of my large HTML string. The thing about gsub is that it  
will substitute the entire matched string.

Do I need to scan out the ~^LNK.\*?^~, operate on those and then put them  
back into the larger string?

I’m not sure I’m asking this very well, so I apologize if that’s the  
case.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 3, 2006, 1:04am UTC](https://www.ruby-forum.com/t/using-gsub-to-remove-embedded-newlines-in-html-file/101030/2 "2006-08-03T01:04:44Z")

</div>

Something like:

```
@html.scan(/~\^LNK:.*?\^~/mi).each do |link_line|
  new_link_line = link_line.gsub(/[\s\r\n]/, '')
  @html.gsub!(/#{link_line}/mi, new_link_line)
end
```

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 3, 2006, 1:39am UTC](https://www.ruby-forum.com/t/using-gsub-to-remove-embedded-newlines-in-html-file/101030/3 "2006-08-03T01:39:57Z")

</div>

Wes G. wrote:

> Something like:
> 
> ```
> @html.scan(/~\^LNK:.*?\^~/mi).each do |link_line|
> new_link_line = link_line.gsub(/[\s\r\n]/, '')
> @html.gsub!(/#{link_line}/mi, new_link_line)
> end
> 
> ```

This seems to work well:

@html.scan(/~^LNK:.\*?^~/mi).each do |link\_line|  
new\_link\_line = link\_line.gsub(/[\t\r\n]/, ‘’)  
@html.gsub!(/#{Regexp.escape(link\_line)}/mi, new\_link\_line) if  
link\_line != new\_link\_line  
end

I wonder if I could have done with with one @html.gsub!() command, but  
this is much more understandable to me anyway so I’ll stick with this.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/using-gsub-to-remove-embedded-newlines-in-html-file/101030/4 "2006-08-03T14:23:30Z")

</div>

Wes G. wrote:

> This seems to work well:
> 
> @html.scan(/~^LNK:.\*?^~/mi).each do |link\_line|  
> new\_link\_line = link\_line.gsub(/[\t\r\n]/, ‘’)  
> @html.gsub!(/#{Regexp.escape(link\_line)}/mi, new\_link\_line) if  
> link\_line != new\_link\_line  
> end

You can use a block with gsub:  
@html.gsub!(/~^LNK:.\*?~/mi) { |s| s.gsub /\s/, ‘’ }

or something like that.

Good luck.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 3, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/using-gsub-to-remove-embedded-newlines-in-html-file/101030/5 "2006-08-03T22:49:12Z")

</div>

Thanks. That is the _Ruby_ way to do it, and that’s what I wanted to  
know :).

I’ve used blocks with gsub but I keep forgetting that I can put anything  
in there - so far I’ve only used backrefs to pull out pieces of the  
matching regex to rearrange things.

Wes
