You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求匹配非http/https前缀的网站域名的正则表达式

Matching Domains Without http:// or https:// Prefixes

Got it, let's break down how to build a regex that targets domain names not prefixed with http:// or https://—even when they're tucked into longer strings like HTML markup or plain text.

The Core Regex

Here's a solid starting point that works in most modern regex engines (Python, Java, JavaScript ES2018+, etc.):

(?<!https?:\/\/)(www\.[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)

Let's Break It Down

  • (?<!https?:\/\/): This is a negative lookbehind assertion. It ensures that immediately before the domain we're matching, there isn't http:// or https:// (the s? makes the "s" optional to cover both HTTP and HTTPS).
  • www\.: Matches the standard www. prefix (we'll cover adjusting this for non-www domains below).
  • [a-zA-Z0-9-]+: Matches the main domain name (letters, numbers, and hyphens are all valid here per domain naming rules).
  • \.[a-zA-Z0-9-.]+: Matches the top-level domain (TLD) and any nested sub-TLDs (like .co.in or .io). The hyphen at the end accounts for rare TLDs with hyphens, such as .museum.

Example Matches & Non-Matches

What gets matched:

  • www.google.co.in
  • <p>Check out www.w3schools.com for free tutorials</p>
  • My go-to resource is www.abc.com

What doesn't get matched:

  • http://www.google.co.in
  • https://www.w3schools.com/html/
  • <a href="https://example.com">Visit our official site</a> (the href value is ignored since it has the HTTPS prefix)

Adjustments for Edge Cases

If you need to match domains that don't start with www (like example.com), use this modified regex:

(?<!https?:\/\/)([a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)

Just keep in mind this might accidentally match non-domain strings like file.txt or user@domain.com—so if you're strictly targeting website domains, sticking with the www. version is safer unless you add extra checks (like limiting TLD length to 2-6 characters).

Notes on Regex Engine Support

  • For older JavaScript environments (pre-ES2018), negative lookbehinds aren't supported. In that case, you can first match all potential domains and then filter out any that start with http:// or https:// using basic string methods.

内容的提问来源于stack exchange,提问作者biff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:55:14