1

Some possible sting like:

https://www.google.com
End: https://g.co
Inside https://b.co and https://c.co

How to match the URL anywhere in a string? The URL may at the end of a string or end of space inside a string.

Currently I'm using the Regex like:

/(https:\/\/.*?)[$|\s]/gi

But it seems could not match the URL at the end of a string that the '$' does not work here.

lssbq
  • 73
  • 2
  • 12

1 Answers1

1

Try this

/(https?:\/\/\S+)/gi

Or in your way /(https:\/\/.*?)($|\s)/ig

To make things clear, anything inside "[]" already implies a "|". Adding it in "[]" will result in matching the character "|". Also $ inside the "[]" is considered as the char "$" and not as the end of string because of which your regex doesn't match the URL at the end of text.

Hemanth Gowda
  • 604
  • 4
  • 16