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,777
Reaction score
1,200
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,226
Reaction score
1,633
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:

Latest Threads

Latest Posts

Latest Profile Posts

I turned 17 two months ago and I feel like I've grown (personality-wise) more in this 1 year than all the 16 years I had lived combined.
Ugh, I may have been cumbersomely eventing a bunch of stuff I could have easily done changing the tileset "Passage (4 dir)" and "Ladder" options. Oops :D
I have no idea why some of the program "splash screen" always on top. E.g, RMMZ loading screen stuck on the top blocking anything I want to do until it fully loaded. What is even the purpose?
One of the secret places in the game.
Had a dream last night that I died -- what a way to start the weekend! :'D

Forum statistics

Threads
107,639
Messages
1,031,070
Members
139,760
Latest member
MEATLOAFMAN
Top