求可直接匹配非HTML标签内>的正则表达式
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
gflag 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 afterabc - The
>insidesome >text - Both
>characters invfs>>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
相关产品推荐
相关产品推荐

