const regex = /^source=(?P<pg_db_name>[^\s]+)\s*addon=(?P<pg_addon_name>[^\s]+)\s*sample#current_transaction=(?P<pg_transactionID>[^\s]+)\s*sample#db_size=(?P<pg_db_size>[^\s]+)\s*sample#tables=(?P<pg_numebr_of_tables>[^\s]+)\s*sample#active-connections(?P<pg_number_of_active_connections>[^\s]+)\ssample#waiting-connections=(?P<pg_number_of_waiting_connections>[^\s]+)\s*sample#index-cache-hit-rate=(?P<pg_index_cache_hit_rate>[^\s]+)\s*sample#table-cache-hit-rate=(?P<pg_table_cache_hit_rate>[^\s]+)\s*sample#load-avg-1m=(?P<pg_load_avg_1m>[^\s]+)\s*sample#load-avg-5m=(?P<pg_load_avg_5m>[^\s]+)\s*sample#load-avg-15m=(?P<pg_load_avg_15m>[^\s]+)\s*sample#read-iops=(?P<pg_read_iops>[^\s]+)\s*sample#write-iops=(?P<pg_write_ops>[^\s]+)\s*sample#tmp-disk-used=(?P<pg_tmp_disk_used>[^\s]+)\s*sample#tmp-disk-available=(?P<pg_tmp_disk_available>[^\s]+)\s*sample#memory-total=(?P<pg_total_memory>[^\s]+)\s*sample#memory-free=(?P<pg_free_memory>[^\s]+)\s*sample#memory-cached=(?P<pg_cached_memory>[^\s]+)\s*sample#memory-postgres=(?P<pg_memory>[^\s]+)\s*sample#follower-lag-commits=(?P<follower_lag_commits>[^$]+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^source=(?P<pg_db_name>[^\\s]+)\\s*addon=(?P<pg_addon_name>[^\\s]+)\\s*sample#current_transaction=(?P<pg_transactionID>[^\\s]+)\\s*sample#db_size=(?P<pg_db_size>[^\\s]+)\\s*sample#tables=(?P<pg_numebr_of_tables>[^\\s]+)\\s*sample#active-connections(?P<pg_number_of_active_connections>[^\\s]+)\\ssample#waiting-connections=(?P<pg_number_of_waiting_connections>[^\\s]+)\\s*sample#index-cache-hit-rate=(?P<pg_index_cache_hit_rate>[^\\s]+)\\s*sample#table-cache-hit-rate=(?P<pg_table_cache_hit_rate>[^\\s]+)\\s*sample#load-avg-1m=(?P<pg_load_avg_1m>[^\\s]+)\\s*sample#load-avg-5m=(?P<pg_load_avg_5m>[^\\s]+)\\s*sample#load-avg-15m=(?P<pg_load_avg_15m>[^\\s]+)\\s*sample#read-iops=(?P<pg_read_iops>[^\\s]+)\\s*sample#write-iops=(?P<pg_write_ops>[^\\s]+)\\s*sample#tmp-disk-used=(?P<pg_tmp_disk_used>[^\\s]+)\\s*sample#tmp-disk-available=(?P<pg_tmp_disk_available>[^\\s]+)\\s*sample#memory-total=(?P<pg_total_memory>[^\\s]+)\\s*sample#memory-free=(?P<pg_free_memory>[^\\s]+)\\s*sample#memory-cached=(?P<pg_cached_memory>[^\\s]+)\\s*sample#memory-postgres=(?P<pg_memory>[^\\s]+)\\s*sample#follower-lag-commits=(?P<follower_lag_commits>[^$]+)', 'gm')
const str = `source=HEROKU_POSTGRESQL_CYAN addon=postgresql-parallel-49130 sample#current_transaction=1491116935 sample#db_size=70416175775bytes sample#tables=172 sample#active-connections=173 sample#waiting-connections=0 sample#index-cache-hit-rate=0.99768 sample#table-cache-hit-rate=0.99892 sample#load-avg-1m=0.26125 sample#load-avg-5m=0.205 sample#load-avg-15m=0.2175 sample#read-iops=4.0167 sample#write-iops=62.383 sample#tmp-disk-used=33849344 sample#tmp-disk-available=72944943104 sample#memory-total=62879916kB sample#memory-free=578624kB sample#memory-cached=57198556kB sample#memory-postgres=1246028kB
source=HEROKU_POSTGRESQL_YELLOW addon=postgresql-rugged-78838 sample#current_transaction=1624380465 sample#db_size=91403178655bytes sample#tables=176 sample#active-connections=6 sample#waiting-connections=0 sample#index-cache-hit-rate=0.99483 sample#table-cache-hit-rate=0.7344 sample#load-avg-1m=0.0075 sample#load-avg-5m=0.005 sample#load-avg-15m=0 sample#read-iops=0 sample#write-iops=0.59322 sample#tmp-disk-used=33849344 sample#tmp-disk-available=72944943104 sample#memory-total=31398060kB sample#memory-free=421344kB sample#memory-cached=29831456kB sample#memory-postgres=18036kB sample#follower-lag-commits=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