1

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?

1 Answer 1

5

Try:

heystack.g­sub(/#{n}/­i, '<b>\0</b>')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.