diff --git a/src/utils/editor.ts b/src/utils/editor.ts index c97fde485..ffabb7aa0 100644 --- a/src/utils/editor.ts +++ b/src/utils/editor.ts @@ -45,18 +45,23 @@ export const generatePermlink = (title, random = false) => { }; export const extractWordAtIndex = (text:string, index:number) => { + const END_REGEX = /[\s,]/ let word = ''; - for(let i = index; i >= 0 && text[i] !== ' '; i--){ + for(let i = index; i >= 0 && (!END_REGEX.test(text[i]) || i === index); i--){ if(text[i]){ word += text[i]; } } word = word.split('').reverse().join(''); - for(let i = index + 1; i < text.length && text[i] !== ' '; i++){ - if(text[i]){ - word += text[i]; + + if(!END_REGEX.test(text[index])){ + for(let i = index + 1; i < text.length && !END_REGEX.test(text[i]); i++){ + if(text[i]){ + word += text[i]; + } } } + return word; }