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

如何用正则表达式判断URL中/category/后内容是否以数字开头?

Regex to Detect the Third URL Type

Got it, let's break this down to find the right regex for your needs. You want to flag URLs where the segment right after /category/ starts with one or more digits followed by a hyphen. Here's the regex you can use:

^https://mysite\.com/category/\d+-.*$

Breakdown of each component:

  • ^ : Anchors the match to the start of the string, so we don't accidentally pick up partial matches in longer URLs.
  • https://mysite\.com/category/ : Matches the fixed base path of your URL (note the \.—dots in regex match any character by default, so we escape it to target the actual dot in the domain).
  • \d+ : Captures one or more digits (this is the "123" ID part from your example).
  • - : Explicitly matches the hyphen that comes right after the numeric ID.
  • .* : Matches any remaining characters in the URL (the page title, which can include letters, hyphens, or other valid URL characters).
  • $ : Anchors the match to the end of the string, ensuring there's no extra content after the page title.

Example matches (returns TRUE):

  • https://mysite.com/category/123-page-title ✅
  • https://mysite.com/category/456-another-long-page ✅
  • https://mysite.com/category/789-some-page-with-hyphens ✅

Non-matches (returns FALSE):

  • https://mysite.com/page-title ❌ (missing the /category/ segment)
  • https://mysite.com/category/page-title ❌ (no numeric ID after /category/)
  • https://mysite.com/category/page-123-title ❌ (digits aren't at the start of the segment after /category/)

Quick usage example (JavaScript):

const testUrl = "https://mysite.com/category/123-page-title";
const regex = /^https:\/\/mysite\.com\/category\/\d+-.+$/;
const isThirdType = regex.test(testUrl); // Returns true

If you're using this in another language (Python, PHP, etc.), the core regex stays identical—you just need to adjust the syntax for regex literals or functions slightly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:00:31