I’m trying to write a regular expression that replaces a markdown-style links but it doesn’t seem to be working. This is what I have so far:
# ruby code:
text = "[link me up](http://www.example.com)"
text.gsub!(%r{\[(\+)\]\((\+)\)}x, %{<a target="_blank" href="\\1">\\2</a>})
What am I doing wrong?
We can use the e
xtended option for Ruby’s regex to make it not look like a cat jumped on the keyboard:Note that the above will fail for URLs that have a right parenthesis in them, e.g.
If this is important to you, you replace the
[^)]+with\S+(?=\))which means “find as many non-whitespace-characters as you can, but ensure that there is a)afterwards”.To answer your question “what am I doing wrong”, here’s what your regex said: