Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does using the RegEx /(?<=]),(?=(L|M|J|V))/gi with the JS split method give me this result?

The title explains my problem. I don’t understand why using the RegEx /(?<=]),(L|M|J|V)/gi with the JS split() method on a string gives me an unexpected result.

Using said RegEx results in:

[
'Lunes[9:00-13:00,14:00-16:00]',
  'M',
  'Martes[19:00-3:00]',
  'M',
  'Miercoles[19:00-21:00,0:00-3:30]',
  'J',
  'Jueves[6:00-8:00,8:30-10:30,16:00-20:05]' 
]

instead of:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  'Lunes[9:00-13:00,14:00-16:00]',
  'Martes[19:00-3:00]',
  'Miercoles[19:00-21:00,0:00-3:30]',
  'Jueves[6:00-8:00,8:30-10:30,16:00-20:05]' 
]

My code:

    const pattern = /(?<=]),(?=(l|m|j|v))/gi;
    const myString = "Lunes[9:00-13:00,14:00-16:00],Martes[19:00-3:00],Miercoles[19:00-21:00,0:00-3:30],Jueves[6:00-8:00,8:30-10:30,16:00-20:05]";
    const myArray = myString.split(pattern);
    console.log(myArray);

>Solution :

It is due to the capture group in the lookahead, which you can omit
and use a character class instead of an alternation [lmjv]

const pattern = /(?<=]),(?=[lmjv])/gi;
const myString = "Lunes[9:00-13:00,14:00-16:00],Martes[19:00-3:00],Miercoles[19:00-21:00,0:00-3:30],Jueves[6:00-8:00,8:30-10:30,16:00-20:05]";
const myArray = myString.split(pattern);
console.log(myArray);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading