Search code examples
javascriptregexparsingsplitjsonata

regex in a $split() (jsonata) function giving "Regular expression matches zero length string" why?


I'm having a string text like below, i wanna split the text whenever there's a new line AND starts with a date dd/mm/yyyy.

I'm using jsonata $split() function in javascript. However, i get a jsonata error as the following "Regular expression matches zero length string". Please help how can i solve this.

text = "22/03/2012 intro 20/10/2019\n" + 
       "22/03/2012 body\n" + 
       "more body 22/04/2020\n" +
      "22/03/2012 stuff more stuff"


$split(text , /\\r?\\n(?=(?:0[1-9]|[12][0-9]|3[01])[- \\/.](?:0[1-9]|1[012])[- \\/.](?:19|20)\\d\\d)/))
//I want the output to be [ "22/03/2012 intro 20/10/2019",
//                           "22/03/2012 body more body 22/04/2020",
//                           "22/03/2012 stuff more stuff] 
// But my output now is [ "22/03/2012 intro 20/10/2019",
//                           "22/03/2012 body/n + more body 22/04/2020",
//                           "22/03/2012 stuff more stuff]                          

Solution

  • const str = "22/03/2012 intro\n" +
    "22/03/2012 body\n" +
    "more body\n" +
    "22/03/2012 stuff\n" +
    "more stuff\n";
    
    console.log(str.match(/^[0-9][0-9][- /.][0-9][0-9][- /.][0-9][0-9][0-9][0-9][a-zA-Z\s]+(?:[a-zA-Z\s]+)/gm));
    // Will log ["22/03/2012 intro\n","22/03/2012 body\nmore body\n", "22/03/2012 stuff\nmore stuff\n"]