webscopeio / react-textarea-autocomplete

📝 React component implements configurable GitHub's like textarea autocomplete.
MIT License
451 stars 80 forks source link

Invalid Regex when trigger contains multiple characters like (( #185

Closed louisbourque closed 4 years ago

louisbourque commented 4 years ago

Version: 4.6.2

When specifying a trigger like '((', the second '(' character is not escaped in the generated Regex, causing it to be invalid:

Uncaught SyntaxError: Invalid regular expression: /([|\=|(()((?:(?!\1)[^\s])*$)/: Unterminated group

The Regex should be /([|\=|(()((?:(?!\1)[^\s])*$)/

Example: https://codesandbox.io/s/silent-sound-e7uik

The same issue happens when the second character of the trigger is '['

jukben commented 4 years ago

We need to properly escape every of trigger chars. Should be quite straightforward, since we already escape the first one. Do you wanna go for a PR?

louisbourque commented 4 years ago

@jukben I'd be happy to give it a try. I had a look at it some more and it looks like the first character is always escaped. This means that if a trigger character of 's' is used, the trigger won't work. It's interpreted by the Regexp as a space character. Typing a space then another letter causes a TypeError because the trigger is not found:

TypeError trigger[currentTrigger] is undefined

I'll work on a PR, thanks!

jukben commented 4 years ago

Released in https://github.com/webscopeio/react-textarea-autocomplete/releases/tag/v4.6.3 Thanks for contribution!