const regex = /^\s*(\d{1,3})\s+([a-zA-z0-9-_]+\s?[a-zA-z0-9-_]+)\s+(0x[a-zA-Z0-9]{4})\s+(\d{3})\s+(\d{3})\s+(\d+)\s+([a-zA-z0-9-_]+\s?\w+)\s+(\w+)\s+-\s+(\d+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^\\s*(\\d{1,3})\\s+([a-zA-z0-9-_]+\\s?[a-zA-z0-9-_]+)\\s+(0x[a-zA-Z0-9]{4})\\s+(\\d{3})\\s+(\\d{3})\\s+(\\d+)\\s+([a-zA-z0-9-_]+\\s?\\w+)\\s+(\\w+)\\s+-\\s+(\\d+)', 'gm')
const str = `ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE
5 Reallocated_Sector_Ct 0x0033 100 100 010 Pre-fail Always - 0
9 Power_On_Hours 0x0032 096 096 000 Old_age Always - 19033
12 Power_Cycle_Count 0x0032 099 099 000 Old_age Always - 54
170 Unused_Rsvd_Blk_Ct_Chip 0x0032 100 100 010 Old_age Always - 0
171 Program_Fail_Count_Chip 0x0032 100 100 010 Old_age Always - 0
172 Erase_Fail_Count_Chip 0x0032 100 100 010 Old_age Always - 0
173 Wear_Leveling_Count 0x0033 090 090 005 Pre-fail Always - 121
174 Unexpect_Power_Loss_Ct 0x0032 099 099 000 Old_age Always - 12
178 Used_Rsvd_Blk_Cnt_Chip 0x0013 100 100 010 Pre-fail Always - 0
180 Unused_Rsvd_Blk_Cnt_Tot 0x0013 100 100 010 Pre-fail Always - 1618
184 End-to-End_Error 0x0033 100 100 097 Pre-fail Always - 0
187 Uncorrectable_Error_Cnt 0x0032 100 100 000 Old_age Always - 0
194 Temperature_Celsius 0x0032 059 055 000 Old_age Always - 41
199 CRC_Error_Count 0x003e 099 099 000 Old_age Always - 21
233 Media_Wearout_Indicator 0x0013 090 090 000 Pre-fail Always - 15085512
241 Total_LBAs_Written 0x0032 099 099 000 Old_age Always - 19103
242 Total_LBAs_Read 0x0032 099 099 000 Old_age Always - 23403
249 NAND_Writes_1GiB 0x0032 099 099 000 Old_age Always - 61952`;
// 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