基于Quill自定义提及系统:判断字符是否为字母的实现疑问
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
prevCharfor 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

