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

基于Quill自定义提及系统:判断字符是否为字母的实现疑问

Fixing the Non-Alphabet Character Check for Your Quill Mention System

Got it, let's adjust your CheckWord function to stop at non-alphabet characters instead of just spaces. Here's how to do it properly:

First, the key part you need to change is the condition where you check for spaces. Instead of c_char == ' ', we'll use a regex to test if the character is not a letter. For a single character, you can use the regex /^[a-zA-Z]$/ — the ^ and $ ensure we're only matching exactly one letter, no more no less.

Here's the updated function with the modified condition:

CheckWord = function(quill, start){ 
  let at = false, c_char; 
  for(var i = start; i > 0; --i){ 
    c_char = quill.getText(i, 1); 
    // Check if we found a valid @ (preceded by space or start of content)
    if (c_char == '@') { 
      const prevChar = quill.getText(i-1, 1);
      if (prevChar == ' ' || prevChar == '') { 
        at = true; 
        break; 
      } 
    } 
    // Updated condition: stop if current character is NOT a letter
    if (!/^[a-zA-Z]$/.test(c_char)) { 
      at = false; 
      break; 
    } 
  } 
  return at; 
}

Quick breakdown of the changes:

  • Replaced c_char == ' ' with !/^[a-zA-Z]$/.test(c_char) — the ! negates the regex test, so we trigger the break when the character isn't an uppercase or lowercase letter.
  • Added a variable prevChar for readability, but that's optional — you could keep the original inline check if you prefer.
  • This now correctly stops traversing backward whenever it hits any non-alphabet character (like numbers, symbols, spaces, etc.), which is exactly what you need for a proper mention system.

If you want to include accented letters or other Unicode alphabet characters later, you could adjust the regex to /^\p{L}$/u (using Unicode property escapes), but for basic a-z/A-Z, the original regex works perfectly.

内容的提问来源于stack exchange,提问作者Luis felipe De jesus Munoz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:43:21