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

求可直接匹配非HTML标签内>的正则表达式

Solution: Regex to Directly Match > Outside HTML Tags

Got it, let's get this sorted for you! You mentioned the previous regex only returned matches via capture groups, but you want a pattern that directly targets the > characters that aren't enclosed within any valid HTML tags. Here's the regex you need:

/(?:^|<[^>]*>)[^<>]*\K>/g

How It Works

Let's break down each part to understand why this works:

  • (?:^|<[^>]*>): A non-capturing group that matches either the start of the string, or a complete HTML tag (starts with <, ends with >, capturing all content in between). This ensures we're starting our search after a valid tag boundary.
  • [^<>]*: Matches any number of characters that aren't < or > — these are the characters outside of tags.
  • \K: Resets the match start position, so everything before this point is excluded from the final match result.
  • >: The actual character we want to match, now guaranteed to be outside any HTML tag.
  • The g flag ensures we find all such > characters in the string, not just the first one.

Test with Your Sample String

Using your test string (decoded from HTML entities to raw characters):
abc><span>some >text< again some<some tag></some tag>vfs>>vf</span>

This regex will directly match these > characters:

  • The > immediately after abc
  • The > inside some >text
  • Both > characters in vfs>>vf

All > characters within tags (like in <span> or </some tag>) are correctly ignored.

Notes

  • This assumes your HTML tags are properly formed (no unclosed < without a matching >). If you need to handle malformed tags, we'd need to adjust the pattern, but this works for valid HTML as specified in your use case.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:53:08