Regex for URL

/https?:\/\/[A-Za-z0-9][^\s]*[^\s.,;:!?)\]'"]/gi

Options
Matches

Examples

Note

Stops at whitespace and leaves off trailing punctuation, so a URL at the end of a sentence doesn’t take the full stop. Parse candidates with the URL constructor to be sure.

Matches in the example

  • "https://example.com/docs?page=2#intro"
  • "http://localhost:8080"

Token by token

  1. http the text “http”
  2. s? the character “s”, optionally
  3. : the character “:”
  4. \/ the character “/”
  5. \/ the character “/”
  6. [A-Za-z0-9] one of: “A” to “Z”, “a” to “z” or “0” to “9”
  7. [^\s]* any character except a whitespace character, zero or more times, as many as possible
  8. [^\s.,;:!?)\]'"] any character except a whitespace character, “.”, “,”, “;”, “:”, “!”, “?”, “)”, the character “]”, “'” or “"”

Other patterns