1 Reply Latest reply on Mar 25, 2011 10:34 PM by John Hawkinson

    Reference to a word

    Ken Krugh Level 1

      Double clicking on a word very nicely selects that word only and not the punctuation (for instance) following it.


      In VB / CS3 if I use InD.Selection(1).Words(1) it includes from the cursor to the space on either side of the word selecting the quotes that might be around the word as well.


      Is there some other say of getting the word in which the cursor is planted?


      Many thanks,


        • 1. Re: Reference to a word
          John Hawkinson Level 5

          Well, not really. You can defiine what a word is differently and split it up yourself using whatever string functions you would like. This reduces to a "how do I do that in VB" question, though. You could start with a line, but in your case, I think you want to start with the existing definition of a word.


          For instance, in JavaScript, if you just wanted to exclude the bounding punctuation, then you'd use something like:




          which is a regular expresson that matches zero or more non-alphanumerics at the beginning (which are thrown away), followed by any number of any characters, followed by zero or more non-alphanumerics at the end (which are thrown away). With the beginning and ending matches being "greedy," filling as much as they can, and the middle match being non-greedy. And alphanumeric includes underscore.


          I hear VBScript now has regular expressions (who knew?), but they seem to be rather verbose. So you can allegedly use:


          dim word
          set re = New RegExp
          re.Pattern = "/^\W*(.*?)\W*$/"
          word = re.Replace(InD.Selection(1).Words(1), "\1")


          Oh well, at least it doesn't have automatic implied global variables and gratuitous semicolon insertion on line breaks...


          This doesn't seem to have locale support in JavaScript, so it's, err, horribly broken for a word that begins with an accented character, like é.

          I bet this is a place where VB can do a lot better than JS though. Left as an exercise to the reader ;-)