Home >Web Front-end >JS Tutorial >How to Create a Robust Regular Expression for Detecting URLs with or without 'http://' Prefix?

How to Create a Robust Regular Expression for Detecting URLs with or without 'http://' Prefix?

Linda Hamilton
Linda HamiltonOriginal
2025-01-05 21:34:41674browse

How to Create a Robust Regular Expression for Detecting URLs with or without

How to Construct a Comprehensive URL RegEx

RegEx patterns are a powerful tool for matching specific text formats. Identifying a URL using a RegEx pattern requires meticulous attention to ensure accurate detection.

In your case, the difficulty arises when attempting to detect URLs without the "http://" prefix. To address this, here's an enhanced RegEx pattern:

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

Breakdown of the Pattern:

  • Protocol (Optional):

    • https? matches "http://" or "https://" (case-insensitive).
  • Subdomain Prefix (Optional):

    • (www.) matches the "www." subdomain prefix.
  • Domain Name:

    • [-a-zA-Z0-9@:%._ ~#=]{1,256} matches any combination of letters, numbers, and the specified special characters, with a maximum length of 256 characters.
  • Top-Level Domain:

    • .[a-zA-Z0-9()]{1,6} matches the top-level domain (e.g., ".com", ".org") with a maximum length of 6 characters.
  • Path and Query String (Optional):

    • b([-a-zA-Z0-9()@:%_ .~#?&//=]*) captures any additional path elements in the URL.

Alternative Regex without Required Protocol:

[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

This version removes the protocol matching component, making it suitable for URLs like "www.google.com".

The above is the detailed content of How to Create a Robust Regular Expression for Detecting URLs with or without 'http://' Prefix?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn