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,696
Reaction score
1,113
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,093
Reaction score
1,509
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

Are we allowed to post about non-RPG Maker games?
I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:

Forum statistics

Threads
105,884
Messages
1,017,238
Members
137,608
Latest member
Arm9
Top