const regex = new RegExp('\\|poke\\|p([1,2])\\|(.+?)(,|\\|)', 'gm')
const str = `|poke|p1|Volcarona, M|
|poke|p1|Urshifu-*, F|
|poke|p1|Magnezone|
|poke|p1|Tangrowth, M|
|poke|p1|Corviknight, F|
|poke|p1|Hippowdon, F|
|poke|p2|Togekiss, M|
|poke|p2|Kommo-o, M|
|poke|p2|Jirachi|
|poke|p2|Mimikyu, F|
|poke|p2|Dragalge, F|
|poke|p1|Dragapult, F|
|poke|p1|Hatterene, F|
|poke|p1|Flygon, F|
|poke|p1|Inteleon, F|
|poke|p1|Obstagoon, F|
|poke|p1|Arcanine, F|
|poke|p2|Pincurchin, F|
|poke|p2|Magnezone|
|poke|p2|Hawlucha, F|
|poke|p2|Raichu-Alola, M|
|poke|p2|Ferrothorn, M|
|poke|p2|Magearna-Original|
|poke|p1|Slowbro, M|
|poke|p1|Tangrowth, F|
|poke|p1|Crawdaunt, F|
|poke|p1|Dragapult, M|
|poke|p1|Clefable, M|
|poke|p1|Excadrill, M|
|poke|p2|Togekiss, F|
|poke|p2|Starmie|
|poke|p2|Urshifu-*, M|
|poke|p2|Amoonguss, M|
|poke|p2|Cinderace, F|
|poke|p2|Jirachi|`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions