Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:13:38+00:00 2026-05-29T15:13:38+00:00

I’m trying to write a regular expression that replaces a markdown-style links but it

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:13:39+00:00Added an answer on May 29, 2026 at 3:13 pm
    irb(main):001:0> text = "[link me up](http://www.example.com)"
    irb(main):002:0> text.gsub /\[([^\]]+)\]\(([^)]+)\)/, '<a href="\2">\1</a>'
    #=> "<a href=\"http://www.example.com\">link me up</a>"
    

    We can use the extended option for Ruby’s regex to make it not look like a cat jumped on the keyboard:

    def linkup( str )
      str.gsub %r{
        \[         # Literal opening bracket
          (        # Capture what we find in here
            [^\]]+ # One or more characters other than close bracket
          )        # Stop capturing
        \]         # Literal closing bracket
        \(         # Literal opening parenthesis
          (        # Capture what we find in here
            [^)]+  # One or more characters other than close parenthesis
          )        # Stop capturing
        \)         # Literal closing parenthesis
      }x, '<a href="\2">\1</a>'
    end
    
    text = "[link me up](http://www.example.com)"
    puts linkup(text)
    #=> <a href="http://www.example.com">link me up</a>
    

    Note that the above will fail for URLs that have a right parenthesis in them, e.g.

    linkup "[O](http://msdn.microsoft.com/en-us/library/ms533050(v=vs.85).aspx)"
    # <a href="http://msdn.microsoft.com/en-us/library/ms533050(v=vs.85">O</a>.aspx)
    

    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:

    %r{
      \[      # Literal opening bracket   (good)
        (     # Start capturing           (good)
          \+  # A literal plus character  (OOPS)
        )     # Stop capturing            (good)
      \]      # Literal closing bracket   (good)
      \(      # Literal opening paren     (good)
        (     # Start capturing           (good)
          \+  # A literal plus character  (OOPS)
        )     # Stop capturing            (good)
      \)      # Literal closing paren     (good)
    }x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.