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

已有玩家标记匹配正则,如何提取聊天消息正文内容?

Alright, let's figure out how to pull that main message body (like "Hello!" in your examples) now that you've got the regex for catching @-mentioned players. There are two straightforward ways to do this—either stripping out the mention blocks or directly targeting the content after them. Here's how to handle both of your specific scenarios, plus a universal solution just in case:

Option 1: Strip Mentions via Regex Replace

The simplest approach is to remove all the @-player patterns you already identify, then clean up any leftover whitespace.

  • For your first example (space between @ and username: @ benjamin)
    Use a regex that targets the full @ [username] block, then replace all instances with an empty string. Trim the result to get rid of extra spaces:

    const message = "@ benjamin @ tyler @ joe @ nic Hello!";
    const mainBody = message.replace(/@ [a-zA-Z0-9]{1,16}/g, "").trim();
    console.log(mainBody); // Outputs "Hello!"
    

    This works because your original single-player regex can be expanded to match the full @ + username chunk, and the g flag ensures we catch all mentions, not just the first one.

  • For your second example (no space between @ and username: @benjamin)
    Use your existing full-mention regex to target the entire prefix of mentions, then replace and trim:

    const message = "@benjamin @tyler @joe @nic Hello!";
    const mainBody = message.replace(/([@][a-zA-Z0-9]{1,16}[ ]){1,}/, "").trim();
    console.log(mainBody); // Outputs "Hello!"
    

    This regex grabs all consecutive @username blocks in one go, so you only need a single replacement.

Option 2: Directly Match the Main Body

If you prefer to capture the body directly instead of replacing mentions, you can write a regex that skips over all the mention blocks and captures whatever comes after.

  • For the spaced @ format
    Use a non-capturing group to skip all @ [username] blocks, then capture the remaining content:

    /(?:@ [a-zA-Z0-9]{1,16} )*(.*)/
    

    Example usage:

    const match = "@ benjamin @ tyler @ joe @ nic Hello!".match(/(?:@ [a-zA-Z0-9]{1,16} )*(.*)/);
    console.log(match[1].trim()); // Outputs "Hello!"
    
  • For the unspaced @ format
    Similar logic, but adjust the regex to match @username blocks:

    /(?:@[a-zA-Z0-9]{1,16} )*(.*)/
    

    Example usage:

    const match = "@benjamin @tyler @joe @nic Hello!".match(/(?:@[a-zA-Z0-9]{1,16} )*(.*)/);
    console.log(match[1].trim()); // Outputs "Hello!"
    
Universal Solution (Mixed Formats)

If your messages might have a mix of spaced and unspaced @ mentions (like @benjamin @ tyler), use this flexible regex to match any single mention, then replace all instances:

/@ ?[a-zA-Z0-9]{1,16}/g

The ? makes the space after @ optional, so it catches both formats. Example:

const mixedMessage = "@benjamin @ tyler @joe @ nic Hi there!";
const mainBody = mixedMessage.replace(/@ ?[a-zA-Z0-9]{1,16}/g, "").trim();
console.log(mainBody); // Outputs "Hi there!"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:17:28