Skip to content Skip to sidebar Skip to footer

Regex For Detecting Url In Plain Form And In Markdown

I am trying to capture user input in a textarea that might be a url (and similarly email) in any of the three formats - Just plain url. Markdown with title [text](url 'title') Ma

Solution 1:

Firstly, the second regex already works for the third format, so we only need to join the first and second ones.

The simple way to do this is to use the | ("OR") character, like this:

(<firstRegex>)|(<secondRegex>)

Demo

The problem with this is that it mess the capturing groups. If the regex catches the first pattern, the url will be in a different capturing group (4th on my demo) than if it was captured by the second one (2nd group).

Excluding markdown pattern on plain URL regex

Adding (?:^|[^\(\/]) to the beginning of the plain URL pattern will force the regex to match any character that's not a opening parenthesis, thus excluding the markdown case. The url must be extracted using a capturing group, since this character will be included in the match.

Demo

Post a Comment for "Regex For Detecting Url In Plain Form And In Markdown"