Parsing Data in Notes field

autodidact

Warper
Member
Joined
Nov 13, 2020
Messages
3
Reaction score
2
First Language
English
Primarily Uses
RMMV
Hello,
I have been trying to get this JS script just right, but my research and extremely limited regex experience is not helping.

It is very easy to use RPG's meta field, unfortunately my tags do not allow me to utilize the "meta" property since they use the same key name.

These are the tags I want to use:
Code:
<sal_step: 1, sal_pavedFootsteps, 100>
<sal_step: 2, sal_pavedFootsteps, 150>
This is the code I am using to attempt parsing:
JavaScript:
    var regexPattern = "/<sal_step: (.*)>/i";
    var notesAsString = $gameMap.tileset().note; //gets note data
    var arrayOfNotes = notesAsString.split(regexPattern); //should split into an array
    
    var result = arrayOfNotes[0];
    console.log(result); //this sadly outputs all note data as a single string, instead of a string array value

I have tried using "/<sal_step: (.*)>/i" as my regex pattern but this grabs both tags in one string. I don't know what I am doing wrong, but until I can break the tags into an array of strings I won't be able to start parsing for data.

Please help! (Using RPG MV Maker 1.6.1, on Debian 9 Linux)
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,773
Reaction score
1,193
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi!
How about making a substring from the note tag you want, then apply the regex separately on each one?
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,222
Reaction score
1,630
First Language
EN
Primarily Uses
RMMZ
I have tried using "/<sal_step: (.*)>/i" as my regex pattern but this grabs both tags in one string.
JavaScript's regular expressions are greedy by default, i.e. they return the longest possible match. In this case I'd suggest matching a character set rather than any character, e.g. /<sal_step:([^>]*)>/ig (any character except >).

Suggested approach to get an array of all <sal_step> notetag values:
JavaScript:
let res = [...$gameMap.tileset().note.matchAll(/<sal_step:([^>]*)>/ig)].map(m => m[1]);
You might need a different approach if you want compatibility with older versions of RMMV, since this uses some newer features. Possible alternative:
JavaScript:
var regex = /<sal_step:([^>]*)>/ig;
var str = $gameMap.tileset().note, res = [], match;
while (null !== (match = regex.exec(str))) {
  res.push(match[1]);
}
More details on regular expressions can be found here~

Edit: if you're OK using a single big notetag like Eliaquim suggested then you can avoid the regular expression stuff entirely, e.g.
JavaScript:
($gameMap.tileset().meta.sal_step || '').split('|');
If applied to <sal_step:apple,1|banana,2|carrot,3> this would return a 3-element array of strings:
["apple,1", "banana,2", "carrot,3"]
You could split on a different character if desired, e.g. \n (new line).
 
Last edited:

autodidact

Warper
Member
Joined
Nov 13, 2020
Messages
3
Reaction score
2
First Language
English
Primarily Uses
RMMV
Hi!
How about making a substring from the note tag you want, then apply the regex separately on each one?
With your post and Caethyril's example combined, I am able to get it to output exactly what I want! :)
JavaScript:
    var notesAsString = $gameMap.tileset().note.split('\n');
This lets me put each tag into an array, if I separate each <> by a newline. Are there any downsides to this over regex?

Thanks for the answer!

Hi Caethyril,
Okay, I just tried your code. And let me start with thank you very much!

Your edit to demonstrate Eliaquim's answer was wonderful! I successfully used it with a split(\n).

Also, I literally copied and pasted your regex code and it also worked perfectly. Thank you for helping me to understand this, I understand regex a lot better having code that actually runs!

I just want to add that my regex code wasn't working because I had it written as: "/<sal_step: (.*)>/i";

The quotes around it made it appear as a string, not a regex expression. While the expression was still off, taking it out of quotes made it work as a regex.
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Who would think putting a power button on a side of a laptop is a good thing? You want to move the laptop and you suddenly press the button.
The second draft of my script is almost done, and I don't think I'm going to write a third draft. I'll make changes to dialogue here and there, but I'm happy with the series of events the way they are.
How you feel about font and dialogue box changing for every main character while the minor cast and regular npc get their own styles? Too much?
Just found an obviously custom effect (i.e. edited by me) in my resources>sounds folder called "rip jaw off". Dear Lord what is wrong with me as a person that at some point I needed a sound *specifically* for THAT??
(...rhetorical question.)

Forum statistics

Threads
107,526
Messages
1,030,277
Members
139,652
Latest member
jasasewamobilbali
Top