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

如何调整正则表达式仅移除前置无其他单词的XXX?

Solution for Targeted XXX Removal

Got it, let's adjust your regex to only remove XXX when it appears at the start of the string (with no preceding words). Here's how to make it work exactly as you need:

The Adjusted Regex

Use this pattern for matching, and replace matches with the captured group $1:

(?i)^(\s*)\bXXX\b

Pattern Breakdown

Let's walk through why this fits your requirements perfectly:

  • ^: The line-start anchor ensures we only target XXX when it’s at the very beginning of the string (no other words come before it)
  • (?i): Retains the case-insensitive matching behavior from your original regex, so it works for xxx, Xxx, etc.
  • (\s*): A capture group that matches any leading whitespace (spaces, tabs, etc.). We preserve this in the replacement so leading spaces aren’t accidentally removed
  • \bXXX\b: Matches the exact word XXX — the \b word boundaries prevent partial matches like XXXY or aXXX

Example Results

Let’s verify with your test case and a few more scenarios to confirm behavior:

Input: XXX ABC
Output: ABC

Input: XXX Hello World
Output: Hello World (preserves leading spaces, removes only XXX)

Input: Test XXX Foo
Output: Test XXX Foo (no match, since XXX has a preceding word)

Input: xxx DEF
Output: DEF (case-insensitive matching works as expected)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:14:12