Why does contains or match does not work with two tags?

Discussion in 'Learning Javascript' started by Milena, Aug 6, 2016.

  1. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    Can someone explain this?


    For example, in my event, I did a comment:


    <sound: 4>
    <picture: 6>


    Now, I am told that we can use .contains or .match. WhenI do this:


    if (this.page().list.parameters[0].match(/<sound:\s*(\d+)?>/im)) {
    var sound = this.page().list.parameters[0].match(/<sound:\s*(\d+)?>/im)[1];
    this.eventSoundNumber = Number(sound);
    }


    It won't actually read the value and place it to my new variable eventSoundNumber. In my understanding, it should be able to since it is matching the expression. When I use contains it just do the same. Is there a way to check the <sound: x> tag while keeping <picture: x> or vice versa?
     
    #1
  2. taarna23

    taarna23 Marshmallow Princess Global Mod

    Messages:
    2,349
    Likes Received:
    4,629
    Location:
    Saskatoon, SK, Canada
    First Language:
    English
    Javascript does not have .contains OR .match when dealing with an array (which I assume you are using).


    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods_2


    There IS a .includes and a .findIndex


    However, I was under the impression we couldn't use things introduced in ECMAScript 2016. Is this incorrect?


    Edit: It's worth noting I'm still learning myself, but I'm 99% sure you want includes instead of contains. The rest, I have no clue!
     
    Last edited by a moderator: Aug 6, 2016
    #2
  3. Ossra

    Ossra Formerly Exhydra Veteran

    Messages:
    725
    Likes Received:
    529
    First Language:
    English
    Primarily Uses:
    RMMV
    I just use meta to grab data from notes ... so much easier.

    Code:
    // $dataMap.events[eventId].meta.propName;
    
    $dataMap.events[1].meta.picture;
    $dataMap.events[1].meta.sound;
     
    Last edited by a moderator: Aug 6, 2016
    #3
    Milena likes this.
  4. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A


    OMG this works! thanks Exhydra! It looks easier to use!
     
    #4
  5. Ossra

    Ossra Formerly Exhydra Veteran

    Messages:
    725
    Likes Received:
    529
    First Language:
    English
    Primarily Uses:
    RMMV
    Also, meta is anywhere there is a note. $dataItems, $dataWeapons, and so on. 
     
    #5
  6. Kino

    Kino EIS Game Dev Veteran

    Messages:
    515
    Likes Received:
    672
    Location:
    United States
    First Language:
    English
    Primarily Uses:
    RMMV
    Meta works but be careful.


    Also concerning  match, match doesn't return anything, only whether there is a match or not against the regular expression.


    If you want to get data from using a regular expression.


    You want to use the .exec method.


    After that you need to take your lastIndex property,  and set it to 0 after evaluation. It will get the digit you want from sound if the expression matches.
     
    #6
  7. Ossra

    Ossra Formerly Exhydra Veteran

    Messages:
    725
    Likes Received:
    529
    First Language:
    English
    Primarily Uses:
    RMMV
    Actually, I just realized that meta is saved per event, not per page. So if you want to grab and apply data per page, you will need to use a method similar to the one in the first post. Here is one I just whipped up ... should work alright.


    this.page().list.forEach(function(data) {
    if (data.code == 108 || data.code == 408) {
    var tmpMatch = data.parameters[0].match(/<(sound|picture):\s*(\d+)?>/i);
    if (tmpMatch) {
    switch (tmpMatch[1]) {
    case 'sound':
    this.eventSoundNumber = Number(tmpMatch[2]);
    break;
    case 'picture':
    this.eventPictureNumber = Number(tmpMatch[2]);
    break;
    }
    }
    }
    }, this);


    That will search through the entire page to search for matches.


    I would suggest using more unique identifiers, though. 'sound' and 'picture' are pretty generic, and might be used by another plugin developer on accident.
     
    Last edited by a moderator: Aug 6, 2016
    #7
  8. Shaz

    Shaz Veteran Veteran

    Messages:
    37,576
    Likes Received:
    11,278
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    meta is for notes.  I thought you said you were using comments in an event?


    Try this:

    Code:
    var re = /<sound:\s*(\d+)?>/im;
    var match = re.exec(this.page().list[i].parameters[0]);
    if (match) {
      this.eventSoundNumber = Number(match[3]);
    }
     
    Last edited by a moderator: Aug 6, 2016
    #8
    Mistral, Kino and Milena like this.
  9. Ossra

    Ossra Formerly Exhydra Veteran

    Messages:
    725
    Likes Received:
    529
    First Language:
    English
    Primarily Uses:
    RMMV
    I think @Milena was looking for a way to pass the data ... either notes or comments would work fine. I find notes is easier for simple data passing, but -- for example -- if you want to change the opacity of an event during the setup phase of the page you will need to use comments.
     
    Last edited by a moderator: Aug 6, 2016
    #9
  10. Shaz

    Shaz Veteran Veteran

    Messages:
    37,576
    Likes Received:
    11,278
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Ah.  I just assumed she wanted it in event comments as that's the way the first post is worded.


    Events also have notes, so you can use .meta without having to make something read them.  Unfortunately though, they're all on a single line in a very small-to-read/edit box.  It shouldn't stop you from being able to use .meta on the event, but it'll be a pain to add the notes or to edit them or look through them, without copying it all into a word processor or something.
     
    #10
    Milena likes this.
  11. Ossra

    Ossra Formerly Exhydra Veteran

    Messages:
    725
    Likes Received:
    529
    First Language:
    English
    Primarily Uses:
    RMMV
    Yeah, I agree that entering data into the note field in an event is a pain. I use the field as an 'event global', and comments for 'event local'. As for usage of notes, though ... meta is nice because all of the code is in the base. So, all someone has to do is nab whatever from the 'meta' property.
     
    #11
    Milena likes this.
  12. Milena

    Milena The woman of many questions Veteran

    Messages:
    1,282
    Likes Received:
    106
    Location:
    Ireland
    First Language:
    Irish
    Primarily Uses:
    N/A
    @Shaz - thanks for the suggestion. I might use it when I am going to get comment tag values in the event per page. The thing is sometimes, I jsut feel that comment tags are far extensive so I can add many things into it and have each variable grab their values.


    @Exhydra - Thank you too for the suggestion. I will bookmark this thread, it is very useful for me and I might get back on it again in the future.
     
    #12

Share This Page