I'm trying to do a search in a speech string for the name of a character, more specifically pUser.name.
If I wanted to search for a specific name I could do
if( Speech_Array[currObj].match( /\bWilly\b/ ))
..which would find all instances of Willy in the speech even if there'd be a question mark or exclamation mark behind.
But since I need to search for what can be ANY name, i.e. pUser.name, I need to include pUser.name in the regular expression somehow. I just have no idea how to do that and still make it work
Tried doing stuff like /\b/,pUser.name,/\b/ - but that won't find names which are trailed by exclamation marks or question marks or dots.
If I understand your correctly, you want this behavior:
Xuri wrote:If I wanted to search for a specific name I could do
if( Speech_Array[currObj].match( /\bWilly\b/ ))
..which would find all instances of Willy in the speech even if there'd be a question mark or exclamation mark behind.
But with a variable for the "Willy".
Why not create a character string that contains :
var str = "\b" + puser.name + "\b" ;
Well I could use SubStringSearch - or I could just plain do if (speech_array == pUser.name ) - but that would be messed up if there are exclamation marks or question marks or dots trailing the name, or if the name (or the string i'm generally looking for, not necessarily a name) is also a part of a different word altogether.
For instance, I want it to grab the "Mike" part from "Hello Mike!" - but using standard string-searches doesn't find that unless I specifically look for "Mike!". Also, I don't want the script to trigger on "hi" when someone uses the word "hill".
An example of how I'm setting up the string-searches in cases where I don't use variables:
Well boris, the script already uses onSpeech. But the example you have there will make the script trigger on words like "hailstorm", or other words which has the letters "hail" as a part of it.
I want to avoid that, and the script already takes care of those instances where I'm looking up specific trigger words.
But if I want to check if a player mentions the name of an NPC that's listening to his speech, I can't look for specific trigger words, I have to look for the name of that NPC using myNPC.name or whatever.
There's a simple way of doing that - but which will make the script trigger on words that may accidentally contain the "name" of the NPC. The name "anne" is contained in the word "annex" for instance. To avoid that, I need to be able to specify myNPC.name (or a variable containing that name) in the part of the script where I look up the various words the speech consists of.
Example:
You go up to an NPC and say "Well hello there, Anne!".
Now I could have a trigger word in the script called "anne", and look for it using SubStringSearch( myString, "anne" ) - and it should work reasonably well. But if I then go up to the NPC and say "Do you know where the annex is?" - the script will find the "anne" trigger word in my speech (as a part of "annex"), and trigger the NPC response from that.
To avoid that it seems I must use regular expressions to search for "whole" words. Enclosing the trigger word in \b \b for instance, will make the script only "find" instances of the trigger word where there's nothing infront of it (or an empty space), or if there's a ending behind it (like a comma, or an exclamation mark).
So to look for a trigger word called "anne", I'd use:
SubStringSearch( myString, /\banne\b/ ) - that is, if SubStringSearch can handle regular expressions (no idea, i'm using a different method of searching the speech string - same result though).
Xuri wrote:Enclosing the trigger word in \b \b for instance, will make the script only "find" instances of the trigger word where there's nothing infront of it (or an empty space), or if there's a ending behind it (like a comma, or an exclamation mark).
Actually, that doesn't really do it. \b matches a world boundary. \w matches an alpha character, \W matches a nonword character. \s matches whitespace (same as \t\n\r\f), and \S matches non whitespace
So I think, if you wanted to find the name anywhere (say then put the ! next the name), you would want this:
(\s*?)(\S*?)Willey(\S*?)(s*?) This I think says, 0 or more whitespace, 0 or more characters, then Willey then 0 or more characters, then 0 or more whitespace.
Yeah I know about that - but when I have \bhail\b the script only responds to "hail" when it's written as a stand-alone word, or has a ... nonword? character behind it.
So that part of the script actually does what I want it to
And if I was looking for "Willey" specifically \bwilley\b would do the trick. But I don't know what name I'm looking for, since it's different from NPC to NPC, so I can't use specific searches like that in any case. So I need to find a way of either including the regular expression in the variable in a way that works, or vice versa - including a variable in a regular expression
That's what I'm doing. I split the string on every space, so each array entry is a word. However, extra symbols like exclamation marks and commas and dots and question marks are included in the array entries. That's why I need the regular expression