That’s already a good start!
I need more than hyphenated words, there can also be underscores, dollar signs, slashes, backslashes, brackets… I’m not totally sure yet what should be included
Maybe others are making sense of this, but it’s really not clear to me. How would you expect to tell a regex, which only takes a string, to know anything about a cursor?
Can you give a handful of examples, both input strings and what’s expected to match? That might make it a bit more clear.
The regex must set something like a boundary
Like \w
I’m not so sure how the codemirror 6 function matches the text but it seems like it does it so that it matches from the end of the string to the beginning. I don’t know - I will look into the native functions, then I can tell you more.
What I know is that the example where I got my initial regex (\w*) is from the codemirror programmer himself and he must know what he’s doing ^^
I believe the regex must set such a word-boundary so that this works well but I want to figure all possibilities out
Just to be clear, \w* matches any number of “word” characters, and is entirely equivalent to [a-zA-Z0-9_], which means it matches any digits, any upper- or lower-case characters between a and z, and any underscores (_)… We can write one that adds other characters you want to include by expanding on what’s in the brackets. Often we will have to escape punctuation characters with a single backslash (\).
For example, if we want to include all the characters above as well as these ones: + - * / ? !, we might write /[a-zA-Z0-9_\+\-\*\/\?\!]*/. The trouble with this (or with /\w*/ for that matter), is that they will match all strings, since we allow for zero characters in our matches.
I’m indecisive what I should use at the moment. I feel like a more complicated regex matching all special characters is better but I haven’t tested it
I will test it though and with your help I got an insight how this stuff works. Thank you!
As I understand it, you want to “grab” the last entered auto-completable text snippet when the user presses a keyboard shortcut for autocomplete?
If the last thing the user wrote was “tv-config-toolbar-text”, the regex pattern /\w*/ will only match “text”. To get the whole thing, you need /[-\w]*/.
If you really want to match “all” characters, you could use a negative pattern such as /[^\s]*/, which will capture everything until the first/last whitespace. But do you want to include parentheses, periods and so on?
“If I had only one hour to solve a problem, I would spend up to two-thirds of that hour in attempting to define what the problem is.”