-2

I am using the following regex to match URLs within text

new RegExp(`(https?://\\S+\\.\\S+)\\s`, 'ig');

The problem is when URL appear within brackets like this

(e.g. https://hello.com/fridaysforfuture-eu-socialmedia)

It picks off the closing bracket as part of the URL

My expected behavior is to include Closing bracket in the match if URL is like

https://hello.com/name-(1)

but exclude closing bracket if URL is within brackets like this

(e.g. https://hello.com/fridaysforfuture-eu-socialmedia)

  • 1
    [This](https://regex101.com/r/Id7O7h/1) worked for me: `https?:\/\/\S+\.[^()]+(?:\([^)]*\))*` – JvdV Mar 01 '20 at 15:34

2 Answers2

-1

Try to use :

RegExp("(^|[\s.:;?\-\]<\(])(https?://[-\w;/?:@&=+$\|\_.!~*\|'()\[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',\|\(\).:;?\-\[\]>\)])","i")

Reference: https://mathiasbynens.be/demo/url-regex

Illya
  • 1,268
  • 1
  • 5
  • 16
-1

As far as I'm aware parenthesis are not valid characters to appear in an URL (at least not unencoded)

There are a bunch of RegEx-Patterns for different use-cases in this question:

What is the best regular expression to check if a string is a valid URL?

Roland Kreuzer
  • 912
  • 4
  • 11