0

When I see some text that matches my pattern, I want to create a link to an external site using RedCloth that has a query link for it.

If I have something like:

Text 123 1234 12345

When I see that I want to replace it with:

"Text 123 1234 12345":http://site.com/query?value=Text%20123%201234%2012345

If I let it keep the spaces, RedCloth won't notice this as a link correctly.

Here is where I am at:

s = "this is a string which has Text 123 1234 12345 "

s = s.s.gsub(/(Text \d+ \d+ \d+)/,'"\1":http://site.com/query?value=\1'

=> "Text 123 1234 12345":http://site.com/query?value=Text 123 1234 12345"

The probem is that RedCloth stops parsing after:

 "Text 123 1234 12345":http://site.com/query?value=Text 

So I really need:

"Text 123 1234 12345":http://site.com/query?value=Text%20123%201234%2012345"

Is there a way I can mess with \1 in the right hand side of gsub, such that I could get the following? If not, what's the best way to do this?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Mark Redding
  • 209
  • 1
  • 11
  • Looks like you want `gsub` to escape the spaces: http://stackoverflow.com/questions/1991945/ruby-gsub-function – Narfanator Jun 02 '13 at 21:23
  • Thats a good example, which got me to this one which is how i solved it http://stackoverflow.com/questions/288573/1-and-1-in-ruby – Mark Redding Jun 02 '13 at 21:55

1 Answers1

1

Ok, thanks to the comment by Narfanator I found the following: "$1 and \1 in Ruby".

The solutions was super easy:

s = "this is a string which has Text 123 1234 12345 "
s = s.s.gsub(/(Text \d+ \d+ \d+)/){|x| "\"" + x + "\":https://site.com/query?value=" + CGI::escape(x)}
Community
  • 1
  • 1
Mark Redding
  • 209
  • 1
  • 11