const regex = /\s{2,3}(\d+)\|\s{8,9}(\w+)\|\s{3}(ON\s|OFF)\|(\s+|\s+\w+)\|\s{5}(ON\s|OFF)\n|\s{3}(\d)\s\|\s+(\w+)\s+\|\s+(ON\s|OFF)\|(\s{2}\n|.\w+)|AC current draw\s(\d)\:\s(\d\.\d{2})|Max detected\s(\d\.\d{2})/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\s{2,3}(\\d+)\\|\\s{8,9}(\\w+)\\|\\s{3}(ON\\s|OFF)\\|(\\s+|\\s+\\w+)\\|\\s{5}(ON\\s|OFF)\\n|\\s{3}(\\d)\\s\\|\\s+(\\w+)\\s+\\|\\s+(ON\\s|OFF)\\|(\\s{2}\\n|.\\w+)|AC current draw\\s(\\d)\\:\\s(\\d\\.\\d{2})|Max detected\\s(\\d\\.\\d{2})', 'g')
const str = `pshow
Port | Name |Status| Reservation
1 | Fan | ON |
2 | Clim | OFF| mofa
>
����������"
Synaccess Inc. Telnet Session V6.2.
pshow
>
Port| Name |status| Reservation |RB Timer
----+----------------+------+----------------+--------
1| Outlet1| OFF| | ON
2| Outlet2| ON | mofa| OFF
3| Outlet3| ON | | OFF
4| Outlet4| OFF| | OFF
5| Outlet5| ON | | OFF
6| Outlet6| ON | | OFF
7| Outlet7| OFF| | OFF
8| Outlet8| ON | | OFF
9| Outlet9| ON | | OFF
10| Outlet10| OFF| | OFF
AC current draw 1: 0.05. Max detected 0.78 Amps.
Temperature Probe is not installed
>`;
// 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