Whats wrong with this regular expression

Code:
var test = Window_Base.prototype.convertEscapeCharacters
Window_Base.prototype.convertEscapeCharacters = function(text)
{
      text = test.call(this, text);     
      text = text.replace(/\x1bPPIC\<(\d+)\s*,\s*(.+)\>(\{(Scale:\[([-]?\d+){1},([-]?\d+){1}\])?([,]?\s*Opacity:(\d+){1}?)\})?/gi, function()
      {
              var scale = {x:150,y:150};
              var opacity = 255;
              if( arguments[3] )
              {
                  if( arguments[4] )
                  {
                      scale.x = Number(arguments[5]);
                      scale.y = Number(arguments[6]);
                  }
                  if( arguments[7] )
                  {
                      opacity = Number(arguments[8]);
                  }
              }
        // Do something here
        return '';
     }.bind(this));

}

The second show text event is the one which is not working. arguments[3] is not filled for this one and I don't know why?
upload_2019-10-29_9-15-30.png

1st returns Oh this is a tree!!!
2nd returns {Scale:[125,-125]}Oh this is a tree!!!
3rd returns Oh this is a tree!!!
4th returns Oh this is a tree!!!

How can I make the second one returns also only Oh this is a tree!!!
Oh wow that's a convoluted escape sequence... 👋

Well, I tested your RegExp against the test string in the console, and sure enough it only matches "\x1bppic<1,Child>"~
Code:
/\x1bPPIC\<(\d+)\s*,\s*(.+)\>(\{(Scale:\[([-]?\d+){1},([-]?\d+){1}\])?([,]?\s*Opacity:(\d+){1}?)\})?/gi.exec('\x1bppic<1,Child>{Scale:[125,-125]}Oh this is a tree!');

I couldn't spot any obvious mistakes, so rather than try to decipher what's gone wrong, I rewrote it from scratch. This seems to produce the expected matches for me from the test string; assuming it works for you, maybe you can use it to help figure out what was wrong with your expression? 😅
  • Without spacing:
    Code:
    /\x1bPPIC\<(\d+),(.+?)\>(\{(Scale:\[([-]?\d+),([-]?\d+)\])?[,]?(Opacity:(\d+))?\})?/gi
  • With spacing:
    Code:
    /\x1bPPIC\<(\d+)\s*,\s*(.+?)\>(\{(Scale:\[([-]?\d+)\s*,\s*([-]?\d+)\])?[,]?(\s*Opacity:\s*(\d+))?\})?/gi
Thank you very much, it works perfect.
So I know "." means any character, "+" means 0 or more and "?" means 0 or 1, but could you tell me what does the combination ".+?" do?
Great! 😂

By default, regular expressions use a "greedy" algorithm, i.e. they make the longest possible match fitting the expression. Adding a ? immediately after a quantifier (e.g. * or +) forces that match to be "non-greedy", i.e. the shortest possible match fitting the expression. Details can be found here~ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Quantifiers

I've used the non-greedy match here because you could have stuff like this in your message:
Code:
\ppic<1,NPC>Hey, is that...\>\P[0]\<!?
In this case a greedy match might see the separate \> escape character as part of your text match, yielding:
Code:
NPC>Hey, is that...\
...instead of the expected match: NPC. There are other ways around this issue, though, e.g. use an excluded character group instead of a dot: [^>]+. Hope that makes sense! 🙏
Thank you very much.
This information was very useful and I've learned a lot.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/whats-wrong-with-this-regular-expression.114688/