MZ RegEx: whats the difference between "Item:8" and "Item:8" ?

So i started today with advancing my plugin a bit and added a option for multiple item drops. but, for some weird reason, only one of the items got correctly analyzed.
What makes this even weirder is that matchAll can identify the second entry, but regex1.test cannot.
I really need to know what are the edge cases in which this bug can occur, because i get the feeling that this wont be the last time i see this.
Screenshot (1371)_cr.png
Screenshot (1370)_cr.png7zguu616vsaa1.png
Why do you even check for a second time if your list's items match your regex when your list clearly contains only matches? I think you can safely skip the if branch.
From what I can tell, your string spans multiple lines, but you're not using the m (multiline) modifier. As a result, your regex is only matching one of the two lines. Why it chooses to match the second line, I'm not sure, to be honest.

As for why regex1.test("Item:8") gives you both true and false alternatingly, I'm not entirely sure either. What I do know is that the g modifier stands for "global", which means the regex won't stop at the first match, and will instead keep matching until it reaches the end of the string (this might be the reason why only the last line is matched when not using the m modifier). Naturally, the global modifier is necessary if you want to use matchAll().

My guess is that the JavaScript engine isn't properly resetting the regular expression's state, which causes the second test to start from the end of the string. That explanation doesn't really make much sense to me, though, so if you're really curious, I recommend asking on StackOverflow. You might receive an answer from someone who's much more knowledgeable about regex.

So the regex you need is actually:
JavaScript:
const regex1 = /Item:\d+/gm;

By the way, Microsoft has a Regular Expressions Cheat Sheet PDF which you can download here:

It's actually pretty useful when working with regex.

Just in case the download link breaks, it was found on this page:

Worst case you can search for "regex cheat sheet".
Regular expressions are instanced. In this case your RegExp is marked as global (g flag) so it keeps a note of the last match position:
JavaScript RegExp objects are stateful when they have the global or sticky flags set (e.g., /foo/g or /foo/y). They store a lastIndex from the previous match. Using this internally, test() can be used to iterate over multiple matches in a string of text (with capture groups).
Like Nolonar mentioned, your expression is not multiline, so the first match will be against a blank line. [Edit: apparently not. The state stuff is still relevant, though.]

Source:
Regular expressions are instanced. In this case your RegExp is marked as global (g flag) so it keeps a note of the last match position:
JavaScript RegExp objects are stateful when they have the global or sticky flags set (e.g., /foo/g or /foo/y). They store a lastIndex from the previous match. Using this internally, test() can be used to iterate over multiple matches in a string of text (with capture groups).

Today I learned that a global regex has a state, so it appears my guess wasn't completely wrong.
Today I learned that a global regex has a state, so it appears my guess wasn't completely wrong.
I first thought it was because of some parts of the string look the same besides different UTF-8 code or something like that, i had never in mind that RegEx also have a state XD
www.regex101.com

Just gonna drop this here, it's great for testing pattern matches without having to run an application to get to it.
www.regex101.com

Just gonna drop this here, it's great for testing pattern matches without having to run an application to get to it.
perfect, thanks 😀
From what I can tell, your string spans multiple lines, but you're not using the m (multiline) modifier. As a result, your regex is only matching one of the two lines. Why it chooses to match the second line, I'm not sure, to be honest.

As for why regex1.test("Item:8") gives you both true and false alternatingly, I'm not entirely sure either. What I do know is that the g modifier stands for "global", which means the regex won't stop at the first match, and will instead keep matching until it reaches the end of the string (this might be the reason why only the last line is matched when not using the m modifier). Naturally, the global modifier is necessary if you want to use matchAll().

My guess is that the JavaScript engine isn't properly resetting the regular expression's state, which causes the second test to start from the end of the string. That explanation doesn't really make much sense to me, though, so if you're really curious, I recommend asking on StackOverflow. You might receive an answer from someone who's much more knowledgeable about regex.

So the regex you need is actually:
JavaScript:
const regex1 = /Item:\d+/gm;

By the way, Microsoft has a Regular Expressions Cheat Sheet PDF which you can download here:

It's actually pretty useful when working with regex.

Just in case the download link breaks, it was found on this page:

Worst case you can search for "regex cheat sheet".
Minor correction to this: the m flag only causes ^ and $ to match the beginning/end of individual lines rather than the string as a whole. If the pattern doesn't contain those characters, it has no effect.
I suggest using capturing groups for extracting the number. The regular expression could for example be: const itemRegex = /item:\s*(\d+)/gi
Ref: https://developer.mozilla.org/en-US...Regular_Expressions/Groups_and_Backreferences

Here's a bit of code showing how it can be done practically:
JavaScript:
const itemRegex = /item\s*:\s*(\d+)/gi
let matches = hellsCommonDropList.matchAll(itemRegex)
let itemIds = []
for (const match of matches)
{
    itemIds.push(match[1])
}
// Continue on finding the related item objects using the itemIds array
console.log('itemIds', itemIds)   // \> itemIds ['7', '8']

// matchAll returns 'An iterable iterator (which is not restartable) of matches. Each match is an array with the same shape as the return value of RegExp.prototype.exec().'
// Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll
//
// Essentially this means that you can only iterate over the matches once.
let itemIds2 = []
for (const match of matches)
{
    // This code won't run
    itemIds2.push(match[1])
}
console.log('itemIds2', itemIds2) // \> itemIds2 []
Note: I added in some optional whitespace matching. Spaces between "item" and ":", and between ":" and the digts are ignored. This makes the matching feel less finicky. I added the Case-insensitive search flag `i` as I don't see any reason why searching for a specific case is needed.

P.s. as Trihan mentioned the multiline `m` flag only matters if the beginning of input `^` or the end of input `$` assertions are used. Here are some useful references to learn more about flags and assertions.
Ref: Flags - https://developer.mozilla.org/en-US...lar_Expressions#advanced_searching_with_flags
Ref: Assertions - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Assertions

Hope this helps you ^^

*hugs*
- Zeriab
I suggest using capturing groups for extracting the number. The regular expression could for example be: const itemRegex = /item:\s*(\d+)/gi
Ref: https://developer.mozilla.org/en-US...Regular_Expressions/Groups_and_Backreferences

Here's a bit of code showing how it can be done practically:
JavaScript:
const itemRegex = /item\s*:\s*(\d+)/gi
let matches = hellsCommonDropList.matchAll(itemRegex)
let itemIds = []
for (const match of matches)
{
    itemIds.push(match[1])
}
// Continue on finding the related item objects using the itemIds array
console.log('itemIds', itemIds)   // \> itemIds ['7', '8']

// matchAll returns 'An iterable iterator (which is not restartable) of matches. Each match is an array with the same shape as the return value of RegExp.prototype.exec().'
// Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll
//
// Essentially this means that you can only iterate over the matches once.
let itemIds2 = []
for (const match of matches)
{
    // This code won't run
    itemIds2.push(match[1])
}
console.log('itemIds2', itemIds2) // \> itemIds2 []
Note: I added in some optional whitespace matching. Spaces between "item" and ":", and between ":" and the digts are ignored. This makes the matching feel less finicky. I added the Case-insensitive search flag `i` as I don't see any reason why searching for a specific case is needed.

P.s. as Trihan mentioned the multiline `m` flag only matters if the beginning of input `^` or the end of input `$` assertions are used. Here are some useful references to learn more about flags and assertions.
Ref: Flags - https://developer.mozilla.org/en-US...lar_Expressions#advanced_searching_with_flags
Ref: Assertions - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Assertions

Hope this helps you ^^

*hugs*
- Zeriab
While I already knew about the capturing groups, I think it would be pretty useful for me if I figure out how to utilize Named capturing groups, which would allow me to separate additional factors easier, like, for example, the weight of a drop. The whitespace thing (\s*) was something I somehow overlooked, that's pretty useful.

Actually, Today I was able to get the basic functionality of my plugin working! It's pretty bare-bones, but it does what it's supposed to do.

Original thread: https://forums.rpgmakerweb.com/threads/regex-whats-the-difference-between-item-8-and-item-8.153891/