const regex = /\[([^\].]*)\]:\W?(.*)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\[([^\\].]*)\\]:\\W?(.*)', 'g')
const str = `[12:17:05][econ]: cid=0 authed
[12:17:11][server]: player is ready. ClientID=0 addr=87.100.133.110:64762
[12:17:11][game]: Teams are balanced (red=1 blue=0)
[12:17:11][server]: player has entered the game. ClientID=0 addr=87.100.133.110:64762
[12:17:11][game]: team_join player='0:wavi' team=0
[12:17:13][game]: pickup player='0:wavi' item=3
[12:17:25][game]: pickup player='0:wavi' item=2
[12:17:33][game]: flag_grab player='0:wavi' team=0
[12:17:34][game]: pickup player='0:wavi' item=1
[12:17:34][game]: pickup player='0:wavi' item=1
[12:17:43][game]: pickup player='0:wavi' item=0
[12:17:44][game]: flag_capture player='0:wavi' team=0 time=11.38
[12:17:48][game]: kill killer='0:0:wavi' victim='0:0:wavi' weapon=-1 special=0
[12:17:52][server]: client dropped. cid=0 addr=87.100.133.110:64762 reason=''
[12:17:52][game]: kill killer='0:0:wavi' victim='0:0:wavi' weapon=-3 special=0
[12:17:52][game]: leave player='0:wavi'
[12:17:52][game]: Teams are balanced (red=0 blue=0)
[econ]: cid=0 authed
[server]: player is ready. ClientID=0 addr=87.100.133.110:64762
[game]: Teams are balanced (red=1 blue=0)
[server]: player has entered the game. ClientID=0 addr=87.100.133.110:64762
[game]: team_join player='0:wavi' team=0
[game]: pickup player='0:wavi' item=1
[game]: pickup player='0:wavi' item=1
[game]: pickup player='0:wavi' item=1
[game]: pickup player='0:wavi' item=2
[game]: pickup player='0:wavi' item=4
[game]: flag_grab player='0:wavi'
[game]: pickup player='0:wavi' item=2
[game]: pickup player='0:wavi' item=0
[game]: pickup player='0:wavi' item=3
[game]: flag_capture player='0:wavi'
[game]: pickup player='0:wavi' item=2
[game]: kill killer='0:wavi' victim='0:wavi' weapon=-1 special=0
[server]: client dropped. cid=0 addr=87.100.133.110:64762 reason=''
[game]: leave player='0:wavi'
[game]: Teams are balanced (red=0 blue=0)`;
// 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