0

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
  • 63
  • 1
  • 11
  • 1
    Possible duplicate of [Regular Expression to find URLs in block of Text (Javascript)](https://stackoverflow.com/questions/1756543/regular-expression-to-find-urls-in-block-of-text-javascript) – Always Sunny Apr 27 '18 at 02:05

1 Answers1

0

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
  • 465
  • 3
  • 15