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,772
Reaction score
1,192
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,219
Reaction score
1,629
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

oh frisk i wont be able to jog for at least 2 weeks ;-; its like a nightmare
It was my birthday yesterday woooo /) ~ 3 ~)/)~~
Around 5am and can't sleep...It happens! Been a productive night at least!
Got my portraits working with a plugin, rather then "Show Picture" since that was too long of a process and I'm sure would have ended up with massive lag
To composers out there, what are atleast 4 instruments overall I need to have to make my music not feeling empty? I'm here stuck trying to make my own battle music but it kinda sounds... Not intense?
My computer broke yesterday, so no RPG maker for at least a week until I can get it repaired. :(

Forum statistics

Threads
107,411
Messages
1,029,494
Members
139,523
Latest member
kid4twenty
Top