const regex = /(?<action>[a-zA-Z]+)@\/(?<dev_path>.*)\/(?<subsystem>[a-zA-z]+)\/(?<name>[a-zA-z]+)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<action>[a-zA-Z]+)@\\\/(?<dev_path>.*)\\\/(?<subsystem>[a-zA-z]+)\\\/(?<name>[a-zA-z]+)', 'g')
const str = `change@/devices/soc/9000000.usb/power_supply/usb
change@/devices/soc/9000000.usb/power_supply/usb
change@/devices/virtual/android_usb/android0
change@/devices/soc/99999999.i2c/i2c-3/3-0025/power_supply/battery
change@/devices/soc/9000000.usb/power_supply/usb
change@/devices/soc/9000000.usb/power_supply/usb
change@/devices/soc/soc:usb_charger/power_supply/usb_charger
change@/devices/soc/soc:mains_charger/power_supply/mains_charger
change@/devices/soc/99999999.i2c/i2c-3/3-0025/power_supply/battery
change@/devices/soc/99999999.i2c/i2c-3/3-0025/power_supply/battery
change@/devices/soc/99999999.i2c/i2c-3/3-0025/power_supply/battery
change@/devices/soc/soc:usb_charger/power_supply/usb_charger
change@/devices/soc/soc:mains_charger/power_supply/mains_charger
change@/devices/soc/99999999.i2c/i2c-3/3-0025/power_supply/battery
`;
// 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