No internet connection
  1. Home
  2. How to

Regex JS (null problem)

By Vanessa Garde @Vanessa_Garde
    2023-10-14 12:06:53.196Z

    Hi there,

    I am trying to get my head around for filtering cue numbers (as 1m01, 1M5, 1m10B or 1m7: so basically 1 digit, letter "m or M", another number -with 1 or 2 digits) and an optional ending letter, which can be lowercase or uppercase. I think I got the regex correct but I am not getting the matching option right, and it ouputs "null" always:

    
    var text = "I am working today on 1m10, 2m5 and 3m10B";
    var regex = /\d [mM] \d{1,2}[\w\W]?/g;
    var matches = text.match(regex); 
    // var matches = text.filter(n => n.match(regex)); 
    
    log(matches);
    
    
    // var text = "OEV_1m10_v1a_05060809";
    // let cue = text.match(/\d [mM] \d{1,2}[\w\W]?/);
    
    // var regex = /\d [mM] \d{1,2}[\w\W]?/g;
    // var text = "OEV_1m10_v1a_05060809";
    // let cue = text.match(regex);
    
    // log(text);
    // log(cue);
    

    Could you provide any help on how to get the right string for the "cue" variable? I am trying different things, but no success... I am sure it's a little thing, but I am getting crazy about not getting it right! ;-) Thank you!

    Solved in post #2, click to view
    • 2 replies
    1. @Vanessa_Garde! You just have to remove the spaces in your regex. Remember that those count towards the search. The way you have it now, it's looking for a string that looks like this: "1 m 10".

      Here's the corrected regex:

      var regex = /\d[mM]\d{1,2}[\w\W]?/g;
      
      ReplySolution
      1. VVanessa Garde @Vanessa_Garde
          2023-10-14 17:48:19.269Z

          Great!!! Many thanks @raphaelsepulveda !!! I was getting craaazy!! ;-)
          Always great to get your feedback! Many thanks!!!