URL Regex Javascript Validator

Search...

⌘K

URL Regex Javascript Validator

Search...

⌘K


URL Regex Javascript Validator

URL Regex Javascript Validator

Validate URLs instantly using our URL Regex JavaScript Validator. Whether you’re working with user-submitted forms, scraping data, or handling API calls, this tool helps ensure every link is correctly formatted. It supports http and https, subdomains, query parameters, and more. Pair it with tools like the JavaScript Regex Tester to test patterns live, or use the Token Generator and Base64 Encoder to safely handle encoded URLs and access tokens.

https://www.admin.google.com/
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "https://www.admin.google.com/" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is URL Regex in JavaScript?


In JavaScript, a URL regex (regular expression) is used to check whether a given string follows the structure of a valid web address. This can include HTTP or HTTPS links, domain names, and optional paths or query parameters. Regex helps developers filter out invalid or malformed links in user input, APIs, and forms.


URL regex patterns are useful for:

  • Validating links in forms and user input

  • Extracting URLs from text or logs

  • Cleaning and processing data in web scraping or automation scripts

  • Preventing invalid links from being submitted to servers


Common URL Regex Pattern (JavaScript)


A commonly used regex for basic URL validation is:

/^(https?:\/\/)?([\w-]+\.)+[\w-]{2,}(\/[\w\-._~:/?#[\]@!$&'()*+,;=]*)?$/


This pattern checks:

  • Optional http or https

  • A valid domain name

  • An optional path and query string


How to Validate URLs in JavaScript Using Regex


Here’s how you can use regex to validate a URL in JavaScript:

function isValidURL(url) {
  const urlPattern = /^(https?:\/\/)?([\w-]+\.)+[\w-]{2,}(\/[\w\-._~:/?#[\]@!$&'()*+,;=]*)?$/;
  return urlPattern.test(url);
}

// Test
console.log(isValidURL("https://example.com"));           // true
console.log(isValidURL("http://my.site/path?q=1"));       // true
console.log(isValidURL("invalid-url"));                   // false


Examples of Valid and Invalid URLs


Input

Valid?

https://example.com

Yes

http://sub.domain.co.uk

Yes

ftp://invalid.protocol.com

No

example..com

No


Pro Tips for Using URL Regex in JavaScript


  • Use non-capturing groups when you don’t need to extract sub-matches. It improves performance.

  • Combine this with the JavaScript Regex Tester to test complex patterns interactively.

  • Avoid overly strict patterns—some valid URLs might be blocked unintentionally.

  • Normalize input before validation (e.g., trimming whitespaces).

  • Pair it with tools like the Token Generator to validate URLs containing access tokens or the Base64 Encoder for secure URL components.


Regex Metacharacters Used 


  • ^ : Anchors the match at the start of the string

  • $ : Anchors the match at the end of the string

  • ? : Marks the preceding token as optional

  • () : Capturing group

  • (?:): Non-capturing group

  • [] : Character set

  • . : Matches any character except newline

  • + : Matches one or more occurrences

  • * : Matches zero or more occurrences

  • \/ : Escaped slash (for path components)


Combine with These Tools


Frequently asked questions

What does a valid URL look like?×
A valid URL starts with http:// or https:// followed by a domain name and optionally paths or query parameters.
Can I use this validator to check IP-based URLs?+
Does this regex check if the URL actually exists?+
Can it handle subdomains and TLDs like .co.in?+
Why doesn’t ftp:// work with this pattern?+