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

JavaScript正则匹配URL查询参数但排除HTML实体问题求助

Fixing Your URL Regex to Handle & Edge Cases

Got it, let's figure out why your current regex isn't behaving as expected and fix it up to meet your requirements.

What's Wrong With the Original Pattern

Your regex /(?:(?:https?):\/\/[-A-Z0-9+@#\/%?=~_|!:,.;]*(?:&(?!lt]))*)/gi has two critical issues:

  • Missing & in the character set: The initial character class [-A-Z0-9+@#\/%?=~_|!:,.;] doesn't include &, so it stops matching as soon as it hits the & in &. That's why it only gets up to https://a.com?a=b& instead of including the rest of the URL.
  • Incorrect lookahead check: The negative lookahead (?!lt]) is checking for lt] (a closing bracket after lt), but you need to stop when & is followed by lt;. This is why it doesn't terminate early for https://a.com<.

The Fixed Regex

Here's a revised pattern that addresses both issues and meets your expected behavior:

/(https?:\/\/(?:(?!<).)*)/gi

How This Works

Let's break down the key parts:

  • https?:\/\/: Matches the standard http:// or https:// URL prefix.
  • (?:(?!<).)*: This non-capturing group acts as a "stop when we see <" guard:
    • (?!<): A negative lookahead that checks if the next characters are not <.
    • .: Matches any single character (excluding newlines; use [\s\S] instead if you need to include line breaks).
    • *: Repeats this check for every character until we hit < or the end of the string.

If you want a stricter version that only allows valid URL characters (instead of matching any character), use this pattern instead:

/(https?:\/\/[-A-Z0-9+@#\/%?=~_|!:,.;&]*(?:&(?!lt;)[-A-Z0-9+@#\/%?=~_|!:,.;&]*)*)/gi

This version restricts matches to common URL-safe characters and explicitly handles & segments that aren't followed by lt;.

Testing the Fix

Let's verify with your test cases:

  1. Input: https://a.com?a=b&c=d
    • Match Result: https://a.com?a=b&c=d ✅ (full URL is captured, since there's no < to stop at)
  2. Input: https://a.com<
    • Match Result: https://a.com ✅ (stops right before < as intended)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:45:10