Ruby - replacing string via gsub and regexp

How can I replace the string 'nEEdle' to get the following result:

"haystackhaystacknEEdlehaystack" -> "haystackhaystack<b>nEEdle</b>haystack"

In my application I have the search parameter only in lowercase, so I want to take the last regexp result ($~) and use it as the replacement string. The following approach doesn't work:

n = "needle"
haystack.gsub(/#{n}/i, "<b>#{$~}</b>")

Any hints?

Answers


Try:

heystack.g­sub(/#{n}/­i, '<b>\0</b>')

Need Your Help

Upload image from string

image forms upload

If i have a filename for a local file on the computer:

how to create a filterable list with javascript based on 3 select boxes

php javascript html wordpress

I need to method to implement something similar to what is on this webpage

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.