Shoutbox

[HELP]match New question Solved! - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [HELP]match New question Solved! (/showthread.php?tid=88667)

[HELP]match New question Solved! by DaAniv on 01-24-2009 at 09:59 AM

code:
for(i=0;i==0;i=i){
    if (sentenceT.match(/\[t=.+?\]/i)=="[t=ps]")
        {
            Time=sentenceT.match(/\[t=.+?\].+?\[\/t\]/i)
            Debug.Trace(Time)
            sentenceT=sentenceT.replace((/\[t=.+?\].+?\[\/t\]/i),"")
           
        }
    else {i=1}}

well if finds me the string I want but I want Time to equal the string between the [t],[/t] and I don't really know how anyone can help?
RE: [HELP]match by Danny22 on 01-24-2009 at 10:16 AM

Maybe this is what you're trying to do?

code:
Debug.Trace(Time[1])
If not, please let me know.
RE: [HELP]match by DaAniv on 01-24-2009 at 11:10 AM

no Time=[t=ps]bla bla[/t] and I want Time="bla bla"


RE: [HELP]match by Matti on 01-24-2009 at 11:11 AM

Aah, here comes on of the most beautiful features about JScript's replace function: you can use a regular expression combined with a custom replacement handler function!

First of all, you need to put what you want to find between parentheses in your regular expression. This way, the engine will capture these parts and pass them as parameters to your replace function or as properties of the global RegExp object (e.g. RegExp.$1). This will give you almost infinite control about how the replacing is done since you can do anything inside your handler, and it is something I use a lot in my scripts too.

Here is your code adapted with String.replace. As you can see, I got rid of your for-loop (in fact, you were better off with a while-loop there) and I made an example replacement handler function.

Javascript code:
sentenceT = sentenceT.replace(
    /\[t=(.+)\](.*?)\[\/t\]/gi, //The regular expression, note the parentheses to capture parts of our interest
    function(sMatch, $1, $2, nOffset, sFull) {  //We can use a function to do the replacing!
        if($1 === "ps") {
            //We found a [t=ps] match
            Time = $2;  //Set the Time variable to what's between the tags
            return "";  //Return an empty string to remove this match from the result
        }
        //Return the matched piece untouched if nothing was recognized
        return sMatch;
    }
);

Of course, if you have for instance 3 capture parts in your regular expression, you should place 3 receiving parameters in your handler function. Alternatively, you can leave out the last parameters (nOffset and sFull) if you don't need them.
RE: [HELP]match by DaAniv on 01-24-2009 at 11:22 AM

Thanks a lot it works


RE: [HELP]match Solved by Danny22 on 01-24-2009 at 11:38 AM

Sorry, when I said Time[1], I also meant this:

code:
Time = sentenceT.match(/\[t=.+?\](+?).\[\/t\]/i)
But it seems like you got your answer already. :)
RE: [HELP]match new question by DaAniv on 01-24-2009 at 12:19 PM

code:
a = Math.floor((Math.random()*(Nouns.length-1)));
b = Math.floor((Math.random()*(VerbsES.length-1)));
d = Math.floor(((Math.random()*16)+14));
e = Math.floor((Math.random()*(Places.length-1)));
f = Math.floor((Math.random()*(PlacesPrepos.length-1)));
g = Math.floor((Math.random()*(Adjectives.length-1)));
//person
if (string.match(/!person/i)){
    c = Math.floor((Math.random()*(Person.length-1)));
    string=string.replace(/!person/i, Person[c])}
//verb
if (!(c==0||c==1||c==2||c==3||string.match(/!i/i)||string.match(/!you/i)||string.match(/!we/i)||string.match(/!they/i)))
    string=string.replace(/!verbs/, VerbsES[b])
else
{
    string=string.replace(/!verbs/i, Verbs[b])
    string=string.replace(/!/g,"")
}
well I want It replace more then one time but every time different number
e.g
string=!person !person
and every person is different but I don't want to use useless loops or anything
RE: [HELP]match New question by Matti on 01-24-2009 at 12:49 PM

Well, as I said, you can do anything you want in your replacement handler! Only this time, we don't need any of the parameters passed to our handler function.

Javascript code:
var c;
string = string.replace(/!person/i, function() {
    c = Math.floor((Math.random()*(Person.length-1)));
    return Person[c];
});

From what I understand of your script now, you're working on a random sentence generator. The problem now is that you're replacing "!person" more than once and you need to find the correct conjugation of the verb for each instance of a person, which may be a tough challenge...
RE: [HELP]match New question by DaAniv on 01-24-2009 at 01:44 PM

ya spot on but It needs to set a verb after the person and then come back to check if theres a new !person


RE: [HELP]match Solved by DaAniv on 01-24-2009 at 02:49 PM

Solved D: thanks to everyone who helped me^_^

Javascript code:
//person
            string.replace(/!person/mig, function()
                    {
                        c = Math.floor((Math.random()*(Person.length-1)));
                        string = string.replace(/!person/mi, Person[c])
                        //verb
                    string = string.replace(/(.*?)!person/mi,function(sMatch, $1)
                    {
                        substr=$1
                        if (!(c==0||c==1||c==2||c==3||string.match(/!i/mi)||string.match(/!you/mi)||string.match(/!we/mi)||string.match(/!they/mi)))
                            substr=substr.replace(/!verbs/mgi,function()
                            {  
                            b = Math.floor((Math.random()*(VerbsES.length-1)));
                            return VerbsES[b]})
                        else
                            substr=substr.replace(/!verbs/mgi,function()
                            {  
                            b = Math.floor((Math.random()*(Verbs.length-1)));
                            return Verbs[b]})
                        return substr+"!person"
                    });
                    return string
                });
            //verb
            string = string.replace(/!verbs/mgi,function()
            {
                b = Math.floor((Math.random()*(VerbsES.length-1)));
                if (!(c==0||c==1||c==2||c==3||string.match(/!i/mi)||string.match(/!you/mi)||string.match(/!we/mi)||string.match(/!they/mi)))
                    return VerbsES[b]
                else
                    return Verbs[b]
            });