MV I am struggling with Regular Expressions

I am struggling so much with this. Please help me out. I have inputted <hi met: 0> at my Attack skill. I am trying to access it via Regular Expression just like Yanfly. Well I have done it copying Yanfly but it doesn't read the number. It prints out "Hi Met NaN". Please ignore all other code here. A lot of it is for testing purposes.

Spoiler: Code
JavaScript:
_Game_Action_apply = Game_Action.prototype.apply;
Game_Action.prototype.apply = function (target) {
    _Game_Action_apply.call(this, target);
    console.log("EVALUATE");
    console.log(this._item);
    if (this._item._dataClass == 'skill') {
        var skill = $dataSkills[this._item._itemId];
        console.log(skill);
        if (skill.meta.unlock) console.log("UNLOCK");
        let note = Object.keys(skill.meta);
        console.log(note);
        let note2 = "/<(?:hi met):\s(\d+)>/ig"
        let skillMeta = skill.meta;
        console.log(skillMeta);
        //console.log(note2 + " " + RegExp.$2);
        if (note2.match(skillMeta)) {
            console.log("Hi Met " + parseInt(RegExp.$1)); //this one is troublesome
        }
    }
    //if(this._item.meta)
}
"NaN" means "not a number",..
in this case that means that whatever this should store: "parseInt(RegExp.$1));"
is returning a text ..
in some cases i solved this by using "number":
JavaScript:
var exampleVariable = 0;
if (isNaN(exampleVariable)) exampleVariable = Number(exampleVariable);
-> this will only work if that variable is able to return as number..

this can be made simpler, i just wanted to show that "Number()" forces its content to return as number if possible.. and i wanted to show how to check "if NaN"

explanation, in some cases the variable content can be a number or another variable which was used to store something.. and the system doesnt know if you want to return the text(=variable storage).. or the final number that is stored inthere..

Most people will start the storing variable as "0". like above to avoid such thing, because in such case the System defines it as number by default

->thats why sometimes "0"(number) is used as empty placeholder and in other cases "null"(text) is used as empty placeholder
@dopan Thanks, "Number(RegExp.$1)" has worked immediately!! IDK how I couldn't figure this out.
Number(RegExp.$1) may return 0, but that doesn't mean "it works".

The fact that parseInt() returned NaN means that you're not getting the input you expect (in this case "0"), which means you have a bug in your code.

Indeed, I copy-pasted your code into my browser's DevTools, and I had to make a couple of changes to even get it to work at all.

My code was:
JavaScript:
let note2 = /<(?:hi met):\s(\d+)>/ig // a regex is not a string, so it's not wrapped in quote marks (")
let skillMeta = "<hi met: 0>";
if (skillMeta.match(note2)) { // string.match() expects a regex as argument, not the other way around
    console.log("Hi Met " + parseInt(RegExp.$1));
}

My guess as to why your regex didn't work, is that skillMeta = "hi met", which is why note2.match(skillMeta) had a match. But since "hi met" isn't a regex and doesn't contain a capturing group, RegExp.$1 remains empty, and parseInt("") returns NaN.

In other words: your code is absolutely broken and will never work. Proof: try it with <hi met:1> and see if you get "Hi Met 1" instead of "Hi Met 0".

The first step to fixing this is to know what console.log(skillMeta); tells you. Without knowing what's in skillMeta, you're essentially coding blind.
What are you actually trying to do with this?
Number(RegExp.$1) may return 0, but that doesn't mean "it works".

The fact that parseInt() returned NaN means that you're not getting the input you expect (in this case "0"), which means you have a bug in your code.

Indeed, I copy-pasted your code into my browser's DevTools, and I had to make a couple of changes to even get it to work at all.

My code was:
JavaScript:
let note2 = /<(?:hi met):\s(\d+)>/ig // a regex is not a string, so it's not wrapped in quote marks (")
let skillMeta = "<hi met: 0>";
if (skillMeta.match(note2)) { // string.match() expects a regex as argument, not the other way around
    console.log("Hi Met " + parseInt(RegExp.$1));
}

My guess as to why your regex didn't work, is that skillMeta = "hi met", which is why note2.match(skillMeta) had a match. But since "hi met" isn't a regex and doesn't contain a capturing group, RegExp.$1 remains empty, and parseInt("") returns NaN.

In other words: your code is absolutely broken and will never work. Proof: try it with <hi met:1> and see if you get "Hi Met 1" instead of "Hi Met 0".

The first step to fixing this is to know what console.log(skillMeta); tells you. Without knowing what's in skillMeta, you're essentially coding blind.
the code he showed doesnt have "0" in it, i just used "0" as example, we dont know the full coding or the note that is actually used, because he didnt showed it.

i admit that i dont know what "parseInt" does, but i know that i had similar issues when getting note data and transfering it over other text variables, so i assumed that his code had a similar issue..

And i wanted to show how to make sure that the system trys to return a number..

I cant say if his coding actually does what is wanted,in the end ,but if there is any number that can be returned than this could work.
(like i sayed we dont know all the data , or in other words we dont know how the Notetag looks ,because it wasnt shown in the code example)

i myself use simpler coding methods to check & use notetag data.. so i cant give any advice on how "parseInt" works..
@Nolonar Oh, you're right! I have changed the number and it still returned me a 0. But the code you have given me wasn't quite correct either. skillMeta holds the skill's notepad values.

Anyway, I had fixed it and used test and now it works. Here is that code:

let note2 = /<(?:hi met):\s(\d+)>/ig // a regex is not a string, so it's not wrapped in quote marks (") let skillMeta = skill.note.split(/[\r\n]+/); //taken from Yanfly's code console.log(skillMeta); //console.log(skillMeta[3]); for (var a = 0; a < skillMeta.length; a++) if (note2.test(skillMeta[a])) { // string.match() expects a regex as argument, not the other way around console.log("Hi Met " + Number(RegExp.$1)); } }

@Trihan There is no practical use for this at all. I am just using this to learn how to use notes with more than one word there and Regular Expressions.

Thanks again guys!!
I personally find the syntax <(?:text)> is a bit confusing. So, I would just go use a straight regex such as<hi met\s*:\s*(\d+)\s*>. This match with the following variations
  • <hi met: 0>
  • <hi met : 999 >
  • <hi met:12>
  • <hi met :91234>
As for reading the regex, I prefer to use .match() instead of RegExp.$1 for the reason that RegExp.$1 is kinda deprecated and only exists for compatibility (I'm mostly trying to get used). For example

JavaScript:
str = "<hi met: 9>"
match = str.match(/<hi met\s*:\s*(\d+)\s*>/i)
console.log(Number(match[1]))

Both works either way.
@TheoAllen Oooooh that was helpful to me, thanks! I have added a * to \s, so now it's \s*. I have wanted it to consider it to have 0 or more spaces after the colon ( : ) and this does it!

I will try to remember match() too!

Original thread: https://forums.rpgmakerweb.com/threads/i-am-struggling-with-regular-expressions.143248/