Regex to match a URL (http and https)
This pattern finds web links that start with http:// or https:// and runs to the next whitespace or quote. It is meant for extracting links from text, not for strict URL validation.
The pattern
/https?:\/\/[^\s"'<>]+/gThe tester opens with this pattern and the examples below already loaded. Nothing you type is uploaded.
How it works
https?- http, with an optional s.
:\/\/- The literal :// (slashes escaped).
[^\s"'<>]+- Everything up to the next whitespace, quote or angle bracket.
Examples
Matches
https://regexhome.comhttp://example.com/path?x=1&y=2https://sub.example.co.in/a/b#top
Does not match
ftp://example.comwww.example.comexample.com
Use it in JavaScript
const re = /https?:\/\/[^\s"'<>]+/g;
// Find every match in a block of text
const found = text.match(re);
// Validate that a whole value matches (no stray characters around it)
const exact = new RegExp('^(?:' + re.source + ')$', re.flags.replace('g', ''));
const isValid = exact.test(value);Limits and tips
- A link at the end of a sentence picks up the trailing full stop or bracket. Trim trailing punctuation after matching.
- Links without a scheme (www.example.com) are not matched. Add them with an extra alternative if you need them.
- To validate a URL in code, prefer the built-in URL constructor and use this pattern only for extraction.
Frequently asked questions
Should I use a regex or the URL class to validate URLs?
Use new URL(value) inside try/catch to validate. Use a regex like this to find URLs inside free text.
Why does the match include a trailing full stop?
The pattern runs until whitespace. Strip trailing punctuation characters from each match, or add a lookbehind to exclude them.